Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created August 3, 2017 15:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tsubaki/d791dc58f99b59dcb1309e96e573a837 to your computer and use it in GitHub Desktop.
PrefabにScriptableObjectを格納する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using System.Linq;
#endif
public class SData : MonoBehaviour {
[SerializeField] SO mySo;
void Start()
{
Debug.Log (mySo.count);
}
#if UNITY_EDITOR
[ContextMenu("Add", false)]
void Add()
{
GameObject.Instantiate
var so = ScriptableObject.CreateInstance<SO> ();
so.name = "any scriptable object";
mySo = so;
AssetDatabase.AddObjectToAsset(so, AssetDatabase.GetAssetPath(gameObject));
AssetDatabase.SaveAssets ();
}
[ContextMenu("Add", true)]
bool AddValidate()
{
var path = AssetDatabase.GetAssetPath (gameObject);
var isPrefab = PrefabUtility.GetPrefabType (gameObject) == UnityEditor.PrefabType.Prefab;
var hasSO = AssetDatabase.LoadAllAssetsAtPath (path).ToList ().Exists (c => c is ScriptableObject);
return isPrefab && !hasSO;
}
[ContextMenu("Remove", false)]
void Remove()
{
var path = AssetDatabase.GetAssetPath (gameObject);
var so = AssetDatabase.LoadAllAssetsAtPath (path).ToList ().First (c => c is ScriptableObject);
Object.DestroyImmediate (so, true);
mySo = null;
AssetDatabase.SaveAssets ();
}
[ContextMenu("Remove", true)]
bool RemoveValidate()
{
var path = AssetDatabase.GetAssetPath (gameObject);
var isPrefab = PrefabUtility.GetPrefabType (gameObject) == UnityEditor.PrefabType.Prefab;
var hasSO = AssetDatabase.LoadAllAssetsAtPath (path).ToList ().Exists (c => c is ScriptableObject);
return isPrefab && hasSO;
}
#endif
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class SO : ScriptableObject
{
public int count;
}
@tsubaki
Copy link
Author

tsubaki commented Aug 3, 2017

23 34 31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment