Version: 2017.3

描述

ScriptableObject 脚本启动时调用此函数。

Awake is called as the ScriptableObject script starts. This happens as the game is launched and is similar to MonoBehavior.Awake).

下面是一个示例。该示例有两个脚本。第一个是 ScriptableObject 脚本。它实现的代码独立于 MonoBehaviour。第二个是与 MonoBehaviour 相关的小脚本,该脚本访问 ScriptableObject 脚本中的值。

// A ScriptableObject example script.
// The A and B members implement features which
// are unrelated to MonoBehaviour.

using UnityEngine;

public class ScriptObj : ScriptableObject { int a = 10; int[] b = new int[5] {0, 17, 34, 42, 67};

public int A { get {return a; } }

// return value in b array, or -1 if x is out-of-range public int B(int x) { if (x >= 0 &amp;&amp; x <= 5) return b[x]; else return -1; }

public void Awake() { Debug.Log("Awake"); }

public void OnDestroy() { Debug.Log("OnDestroy"); } }

以下脚本使用上述 ScriptableObject 脚本。

// create and access the ScriptObj

using UnityEngine;

public class ScriptObjExample : MonoBehaviour { ScriptObj test;

void Start() { test = (ScriptObj)ScriptableObject.CreateInstance(typeof(ScriptObj));

print(test.A); print(test.B(3)); print(test.B(-3)); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961