Last active
September 3, 2019 19:53
-
-
Save pacoelayudante/8b5d3580f02224b1c3065e90ab2c4d02 to your computer and use it in GitHub Desktop.
Unity: Un attributo para usar en strings, te lo muestra como selector de Asset de Escenas en el Inspector, (pero escribe el path a al escena o el nombre de la escena). ¡Funciona con Arrays!
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; | |
#endif | |
public class SceneAssetPathAsStringAttribute : PropertyAttribute | |
{ | |
readonly bool incluirTodoElPath; | |
public SceneAssetPathAsStringAttribute() : this(false) { } | |
public SceneAssetPathAsStringAttribute(bool incluirTodoElPath) | |
{ | |
this.incluirTodoElPath = incluirTodoElPath; | |
} | |
#if UNITY_EDITOR | |
[CustomPropertyDrawer(typeof(SceneAssetPathAsStringAttribute))] | |
public class SceneAssetPathAsStringAttributeDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
if (property.propertyType != SerializedPropertyType.String) | |
{ | |
EditorGUI.HelpBox(position, "Solo usar con strings", MessageType.Error); | |
return; | |
} | |
var scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(property.stringValue); | |
if (scene == null && !string.IsNullOrEmpty(property.stringValue)) | |
{ | |
string[] guids = AssetDatabase.FindAssets(property.stringValue + " t:scene"); | |
if (guids.Length > 0) scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(AssetDatabase.GUIDToAssetPath(guids[0])); | |
} | |
EditorGUI.BeginChangeCheck(); | |
scene = EditorGUI.ObjectField(position, label, scene, typeof(SceneAsset), false) as SceneAsset; | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
var att = attribute as SceneAssetPathAsStringAttribute; | |
if (att.incluirTodoElPath) | |
{ | |
property.stringValue = AssetDatabase.GetAssetPath(scene); | |
} | |
else | |
{ | |
property.stringValue = scene.name; | |
} | |
} | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment