Last active
October 22, 2019 01:31
-
-
Save starikcetin/6977072a6134152eef5ca8cf891e1936 to your computer and use it in GitHub Desktop.
Unity Edtor-time Prefab Instantiator For Context Menu
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 UnityEditor; | |
using UnityEngine; | |
internal class Instantiator : ScriptableObject | |
{ | |
private static Instantiator _instance; | |
private static Instantiator Instance | |
{ | |
get | |
{ | |
if (_instance == null) | |
{ | |
_instance = CreateInstance<Instantiator>(); | |
} | |
return _instance; | |
} | |
} | |
[SerializeField] private GameObject _prefab; | |
[MenuItem("GameObject/Foo/Bar", false, 12)] | |
private static void Instantiate() | |
{ | |
Selection.activeObject = PrefabUtility.InstantiatePrefab(Instance._prefab); | |
} | |
[MenuItem("GameObject/Foo/Bar", true, 12)] | |
private static bool ValidateInstantiate() | |
{ | |
var go = Instance._prefab; | |
return go != null && PrefabUtility.IsPartOfPrefabAsset(go); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment