Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment
Owner

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