如何腳本間交互:
          方法1:

          通過在編輯器里面拖動,來持有這個對象去調(diào)用對應(yīng)的函數(shù),這個方法比較簡單。

          在編輯器中新建2個腳本。

          我們寫一個a腳本

          public class Ascript : MonoBehaviour {

          // Use this for initialization

          void Start () {

          }

          // Update is called once per frame

          void Update () {

          }

          public void DoSomething()

          {

          Debug.LogWarning ("Ascript .. DoSomething .. Call");

          }

          }

          我們想Main腳本去調(diào)用A腳本。我們就在Main 寫一個A對象。

          public Ascript ascript_;

          這個是在代碼編寫的時候已經(jīng)可以調(diào)用A對象的public 函數(shù)。

          if (ascript_)

          ascript_.DoSomething ();

          但其實這個ascript_ 對象是空的,我們要對它賦值。這個時候我們在編輯器拖一個有a腳本的實體對象上去就可以了。

          我們看見Debug.LogWarning ("Ascript .. DoSomething .. Call");有執(zhí)行。

          方法2:

          我們把上面的直接調(diào)用改成

          if (ascript_)

          ascript_.SendMessage ("DoSomething");

          我們把a腳本里面的DoSomething函數(shù)的public 去掉。

          我們看見Debug.LogWarning ("Ascript .. DoSomething .. Call");有執(zhí)行。

          我們嘗試給函數(shù)加一些參數(shù),看看結(jié)果。

          我們對A腳本的 DoSomething 進(jìn)行修改。

          void DoSomething(int param1)

          {

          Debug.LogWarning (string.Format("{0} {1}","Ascript .. DoSomething ..
          Call",param1));

          }

          如果我們腳本中有2個同名的函數(shù)會怎么樣呢。

          測試結(jié)果是誰在上面誰就會被調(diào)用。

          可以在后面加一個參數(shù)。

          if (ascript_)
          ascript_.SendMessage("DoSomething",2,SendMessageOptions.RequireReceiver);

          我們把 a腳本的 DoSomething 函數(shù)刪掉。

          結(jié)果報錯了......

          我們把參數(shù)換了試試。

          if (ascript_)
          ascript_.SendMessage("DoSomething",2,SendMessageOptions.DontRequireReceiver);

          我們發(fā)現(xiàn)不報錯了。

          Public static
          我們a腳本的DoSomething函數(shù)改成

          public static void DoSomething()

          {

          Debug.LogWarning(string.Format("{0} {1}","Ascript .. DoSomething ..
          Call","static function"));

          }

          我們直接 Ascript.DoSomething ();調(diào)用就可以了。

          我們再在這個基礎(chǔ)上面改成下面這個樣子。

          public class Ascript : MonoBehaviour {

          public static Ascript aStatic;

          // Use this for initialization

          void Start () {

          aStatic = this;

          }

          // Update is called once per frame

          void Update () {

          }

          public void DoSomething()

          {

          Debug.LogWarning(string.Format("{0} {1}","Ascript .. DoSomething ..
          Call","static Object"));

          }

          }

          我們把調(diào)用的改成,Ascript.aStatic.DoSomething ();

          我們這個時候已經(jīng)不持有這個對象了。

          但是場景一定要這個對象,如果沒有的話會報錯。

          如果我們場景里面有很多這個對象,他會找到這個對象最早生成的那個,注意不是你創(chuàng)建的實體對象而是第一個拖上這個腳本的對象。

          如何腳本間交互:
          方法1:

          通過在編輯器里面拖動,來持有這個對象去調(diào)用對應(yīng)的函數(shù),這個方法比較簡單。

          在編輯器中新建2個腳本。

          我們寫一個a腳本

          public class Ascript : MonoBehaviour {

          // Use this for initialization

          void Start () {

          }

          // Update is called once per frame

          void Update () {

          }

          public void DoSomething()

          {

          Debug.LogWarning ("Ascript .. DoSomething .. Call");

          }

          }

          我們想Main腳本去調(diào)用A腳本。我們就在Main 寫一個A對象。

          public Ascript ascript_;

          這個是在代碼編寫的時候已經(jīng)可以調(diào)用A對象的public 函數(shù)。

          if (ascript_)

          ascript_.DoSomething ();

          但其實這個ascript_ 對象是空的,我們要對它賦值。這個時候我們在編輯器拖一個有a腳本的實體對象上去就可以了。

          我們看見Debug.LogWarning ("Ascript .. DoSomething .. Call");有執(zhí)行。

          方法2:

          我們把上面的直接調(diào)用改成

          if (ascript_)

          ascript_.SendMessage ("DoSomething");

          我們把a腳本里面的DoSomething函數(shù)的public 去掉。

          我們看見Debug.LogWarning ("Ascript .. DoSomething .. Call");有執(zhí)行。

          我們嘗試給函數(shù)加一些參數(shù),看看結(jié)果。

          我們對A腳本的 DoSomething 進(jìn)行修改。

          void DoSomething(int param1)

          {

          Debug.LogWarning (string.Format("{0} {1}","Ascript .. DoSomething ..
          Call",param1));

          }

          如果我們腳本中有2個同名的函數(shù)會怎么樣呢。

          測試結(jié)果是誰在上面誰就會被調(diào)用。

          可以在后面加一個參數(shù)。

          if (ascript_)
          ascript_.SendMessage("DoSomething",2,SendMessageOptions.RequireReceiver);

          我們把 a腳本的 DoSomething 函數(shù)刪掉。

          結(jié)果報錯了

          我們把參數(shù)換了試試。

          if (ascript_)
          ascript_.SendMessage("DoSomething",2,SendMessageOptions.DontRequireReceiver);

          我們發(fā)現(xiàn)不報錯了。

          Public static
          我們a腳本的DoSomething函數(shù)改成

          public static void DoSomething()

          {

          Debug.LogWarning(string.Format("{0} {1}","Ascript .. DoSomething ..
          Call","static function"));

          }

          我們直接 Ascript.DoSomething ();調(diào)用就可以了。

          我們再在這個基礎(chǔ)上面改成下面這個樣子。

          public class Ascript : MonoBehaviour {

          public static Ascript aStatic;

          // Use this for initialization

          void Start () {

          aStatic = this;

          }

          // Update is called once per frame

          void Update () {

          }

          public void DoSomething()

          {

          Debug.LogWarning(string.Format("{0} {1}","Ascript .. DoSomething ..
          Call","static Object"));

          }

          }

          我們把調(diào)用的改成,Ascript.aStatic.DoSomething ();

          我們這個時候已經(jīng)不持有這個對象了。

          但是場景一定要這個對象,如果沒有的話會報下面的錯。

          如果我們場景里面有很多這個對象,他會找到這個對象最早生成的那個,注意不是你創(chuàng)建的實體對象而是第一個拖上這個腳本的對象。

          單例模式

          public class MyClass : MonoBehaviour {

          void Awake () {

          Debug.Log(Manager.Instance.myGlobalVar);

          }}

          public class Manager : Singleton {

          protected Manager () {} // guarantee this will be always a singleton only -
          can't use the constructor!

          public string myGlobalVar = "whatever";}

          using UnityEngine;

          ///
          /// Be aware this will not prevent a non singleton constructor
          /// such as T myT = new T();
          /// To prevent that, add protected T () {} to your singleton class.
          ///
          /// As a note, this is made as MonoBehaviour because we need Coroutines.
          ///
          public class Singleton : MonoBehaviour where T : MonoBehaviour
          {
          private static T _instance;

          private static object _lock = new object();

          public static T Instance
          {
          get
          {
          if (applicationIsQuitting) {
          Debug.LogWarning("[Singleton] Instance '"+ typeof(T) +
          "' already destroyed on application quit." +
          " Won't create again - returning null.");
          return null;
          }

          lock(_lock)
          {
          if (_instance == null)
          {
          _instance = (T) FindObjectOfType(typeof(T));

          if ( FindObjectsOfType(typeof(T)).Length > 1 )
          {
          Debug.LogError("[Singleton] Something went really wrong " +
          " - there should never be more than 1 singleton!" +
          " Reopening the scene might fix it.");
          return _instance;
          }

          if (_instance == null)
          {
          GameObject singleton = new GameObject();
          _instance = singleton.AddComponent();
          singleton.name = "(singleton) "+ typeof(T).ToString();

          DontDestroyOnLoad(singleton);

          Debug.Log("[Singleton] An instance of " + typeof(T) +
          " is needed in the scene, so '" + singleton +
          "' was created with DontDestroyOnLoad.");
          } else {
          Debug.Log("[Singleton] Using instance already created: " +
          _instance.gameObject.name);
          }
          }

          return _instance;
          }
          }
          }

          private static bool applicationIsQuitting = false;
          ///
          /// When Unity quits, it destroys objects in a random order.
          /// In principle, a Singleton is only destroyed when application quits.
          /// If any script calls Instance after it have been destroyed,
          /// it will create a buggy ghost object that will stay on the Editor scene
          /// even after stopping playing the Application. Really bad!
          /// So, this was made to be sure we're not creating that buggy ghost object.
          ///
          public void OnDestroy () {
          applicationIsQuitting = true;
          }
          }

          更多unity2018的功能介紹請到paws3d爪爪學(xué)院查找。

          友情鏈接
          ioDraw流程圖
          API參考文檔
          OK工具箱
          云服務(wù)器優(yōu)惠
          阿里云優(yōu)惠券
          騰訊云優(yōu)惠券
          京東云優(yōu)惠券
          站點信息
          問題反饋
          郵箱:[email protected]
          QQ群:637538335
          關(guān)注微信

                囯产精品久久欠久久久久久九大 | 日本少妇videos高潮 | 男女拍拍视频网站 | 97国产超碰 | 国产另类专区 |