Unity3D-启动unity事件,切换平台监听

本文介绍了如何在Unity中使用[InitializeOnLoad]属性来实现启动监听,进行版本检测及平台切换监听等操作。此外还提供了代码示例,展示了如何通过EditorUserBuildSettings.SwitchActiveBuildTarget方法切换平台,并监听平台变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1,启动unity监听,针对unity启动监听,可以做些版本检测,以及平台切换监听,可以做些自己想做的操作

[InitializeOnLoad],添加这个标示,标示在unity启动的时候会执行这个脚本

unity官方给的解释是:

Running Editor Script Code on Launch

    Sometimes it is useful to be able to run some editor script code in a project as soon as Unity launches without requiring action from the user.
You can do this by applying the InitializeOnLoad attribute to a class which has a static constructor. 
A static constructor is a function with the same name as the class, 
declared static and without a return type or parameters (see here for more information)
using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
public class Startup {
    static Startup()
    {
        Debug.Log("Up and running");
    }
}

2,代码切换平台方法  EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android)

监听平台切换后方法 

EditorUserBuildSettings.activeBuildTargetChanged = delegate() {
if(EditorUserBuildSettings.activeBuildTarget == BuildTarget.Iphone){
Debug.Log("do you want to do");
}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值