c# - 有没有办法限制脚本只能由 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);
}
}
}
如果我有多个对象的脚本继承自单例类,它将被销毁并且只保留一个对象。我想要的是,如果有办法限制脚本的使用。就像脚本已经附加到游戏对象一样,您不能再将它用于另一个游戏对象,无论是从检查器还是使用拖放操作。
解决方案
推荐阅读
- vue.js - 有没有办法从多个选择中获取数据作为数组?
- json - Newtonsoft JSON Schema - $ref 已解决,但忽略必需
- html - 在列表中动态呈现输入字段
- javascript - 从主数组中获取所有嵌套数组并根据键合并每个数组 Knockout.Js
- hadoop - 如何从配置单元表覆盖到本地目录?
- sql - 将行正确插入 MS SQL Server
- css - 绝对定位的下拉菜单在 IE 中不同
- batch-file - 批处理命令根据值列表检查值
- c# - 如何使用控件 ID 获取 Windows 应用程序文本控件值
- cart - 在 WooCommcerce 中使购物车自动将数量添加到 0 到购物车页面