https://github.com/zeroultra/unityscriptableobjectsingleton
https://github.com/zeroultra/unityscriptableobjectsingleton
plugins unity
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zeroultra/unityscriptableobjectsingleton
- Owner: ZeroUltra
- License: mit
- Created: 2025-01-13T06:36:56.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-13T06:49:17.000Z (over 1 year ago)
- Last Synced: 2025-06-02T13:12:08.178Z (12 months ago)
- Topics: plugins, unity
- Language: C#
- Homepage:
- Size: 1.17 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: .github/README.md
- License: LICENSE
Awesome Lists containing this project
README
# Unity ScriptableObjectSingleton

## 说明
### unity中的ScriptableSingleton
unity中存在一个[ScriptableSingleton](https://docs.unity3d.com/2020.1/Documentation/ScriptReference/ScriptableSingleton_1.html)
但是他有点难用
* hideflag 为 dontsave
* 并不会监听创建时是否已存在
* ....
### 如何使用
* 如果是Editor的`ScriptableObject`可直接继承`ScriptableObjectSingletonEditor`使用,(也可以不继承,但是`必须要实现ISingletion接口`)
* 如果是Runtime的`ScriptableObject`需要实现`ISingletion`接口
示例:
```c#
using System.Collections;
using UnityEngine;
using UnityScriptableObjectSingleton.Editor;
using UnityScriptableObjectSingleton.Runtime;
///
/// ____DESC:
///
[CreateAssetMenu(fileName = "TestScriptableObjectSingleton", menuName = "Test/TestScriptableObjectSingleton", order = 0)]
public class TestScriptableObjectSingleton : ScriptableObject, ISingletion
{
//手动实现Instance
private static TestScriptableObjectSingleton _instance;
public static TestScriptableObjectSingleton Instance
{
get
{
if (_instance == null)
{
_instance = Resources.Load("TestScriptableObjectSingleton");
if (_instance == null)
{
Debug.LogError("Can't find the instance of TestScriptableObjectSingleton");
}
}
return _instance;
}
}
public int value;
}
```
```c#
using System.Collections;
using UnityEngine;
using UnityScriptableObjectSingleton.Editor;
///
/// ____DESC:
///
[CreateAssetMenu(fileName = "TestEditorScriptableObjectSingleton", menuName = "Test/TestEditorScriptableObjectSingleton", order = 0)]
public class TestEditorScriptableObjectSingleton : ScriptableObjectSingletonEditor
{
public int value;
}
```
具体可查看`Samples`里面的示例