首页 > 解决方案 > 有没有办法限制脚本只能由 Unity 中的单个游戏对象使用?

问题描述

我的单例模式代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Singleton<T> : MonoBehaviour where T : Component
{
    private static T instance;

    public static T Instance
    {
        get
        {
            return instance;
        }
    }

    private void Awake()
    {
        if (instance == null)
        {
            instance = this as T;
        }
        else
        {
            Destroy(gameObject);   
        }
    }
}

如果我有多个对象的脚本继承自单例类,它将被销毁并且只保留一个对象。我想要的是,如果有办法限制脚本的使用。就像脚本已经附加到游戏对象一样,您不能再将它用于另一个游戏对象,无论是从检查器还是使用拖放操作。

标签: c#unity3d

解决方案


推荐阅读