csharp Unity中的单身人士

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Unity中的单身人士相关的知识,希望对你有一定的参考价值。

public class UsingSingleton : MonoBehaviour
{
  void Start(){
    // whatever
  }
  
  void Update(){
    // to use the singleton, don't forget the .instance !
    MySingletonInstance.instance.doStuff();
  }
}
public class ControllerManager : Singleton<ControllerManager>
{
  void Start(){
    // will be called the first time it is instanciated.
    // so it behaves like a regular unity object, but will persist
    // accross scenes (as long as one instance is present in each scene)
  }
  
  public void doStuff(){
    // create whatever method you want
  }

}
using UnityEngine;
 using System.Collections;
 
 public class Singleton<Instance> : MonoBehaviour where Instance : Singleton<Instance> {
     public static Instance instance;
     public bool isPersistant;
     
     public virtual void Awake() {
         if(isPersistant) {
             if(!instance) {
                 instance = this as Instance;
             }
             else {
                 DestroyObject(gameObject);
             }
             DontDestroyOnLoad(gameObject);
         }
         else {
             instance = this as Instance;
         }
     }
 }

以上是关于csharp Unity中的单身人士的主要内容,如果未能解决你的问题,请参考以下文章

csharp 从Unity3D中的CoherentUI浏览器获取PCM音频数据,并通过Unity音频过滤器播放。

csharp 单身模式

csharp 单身

csharp Unity获取键盘输入

csharp Unity获取类变量

csharp [WIP] Unity Bezier