Created
August 3, 2017 15:09
Star
You must be signed in to star a gist
PrefabにScriptableObjectを格納する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[CreateAssetMenu] | |
public class SO : ScriptableObject | |
{ | |
public int count; | |
} |
Author
tsubaki
commented
Aug 3, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment