Created
November 10, 2017 01:37
-
-
Save pacoelayudante/0412bf7c8f3d3f8d3166c3089dba9938 to your computer and use it in GitHub Desktop.
Un Scriptable Object (Asset) que basicamente es una lista de Sprites, con un Editor que lo muestra como Reorderable List. Ademas que se puede usar onda "ListaDeImagenes miLista; Sprite miSprite = miLista[2]" y Anda
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
/* | |
* Agradecimientos a | |
* Valentin Simonov https://twitter.com/valyard | |
* Unity: make your lists functional with ReorderableList | |
* http://va.lent.in/unity-make-your-lists-functional-with-reorderablelist/ | |
* | |
* | |
*/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
using UnityEditorInternal; | |
#endif | |
[CreateAssetMenu()] | |
public class ListaDeImagenes : ScriptableObject { | |
public List<Sprite> sprites = new List<Sprite>(); | |
public Sprite this[int index] | |
{ | |
get | |
{ | |
if (sprites == null) return null; | |
else if (index < 0 || index >= sprites.Count) return null; | |
else return sprites[index]; | |
} | |
} | |
public int Count | |
{ | |
get | |
{ | |
if (sprites == null) return 0; | |
else return sprites.Count; | |
} | |
} | |
public void Add(Sprite s) | |
{ | |
sprites.Add(s); | |
} | |
public void Remove(Sprite s) | |
{ | |
sprites.Remove(s); | |
} | |
public static ListaDeImagenes operator +(ListaDeImagenes li, Sprite s) | |
{ | |
li.Add(s); | |
return li; | |
} | |
public static ListaDeImagenes operator -(ListaDeImagenes li, Sprite s) | |
{ | |
li.Remove(s); | |
return li; | |
} | |
} | |
#if UNITY_EDITOR | |
[CustomEditor(typeof(ListaDeImagenes))] | |
public class ListaDeImagenesEditor : Editor | |
{ | |
ListaDeImagenes laLista; | |
Vector2 scrollViewPos; | |
ReorderableList reorderableList; | |
private void OnEnable() | |
{ | |
laLista = target as ListaDeImagenes; | |
reorderableList = new ReorderableList(serializedObject, serializedObject.FindProperty("sprites"),true,true,true,true); | |
reorderableList.drawElementBackgroundCallback = DrawElement; | |
reorderableList.drawHeaderCallback = (Rect rect) => { | |
EditorGUI.LabelField(rect, reorderableList.serializedProperty.displayName); | |
}; | |
} | |
public void DrawElement(Rect r, int index, bool activo, bool focused) | |
{ | |
if (index < 0) return; | |
var element = reorderableList.serializedProperty.GetArrayElementAtIndex(index); | |
EditorGUI.PropertyField(new Rect(r.x+EditorGUIUtility.labelWidth,r.y,r.width - EditorGUIUtility.labelWidth, EditorGUIUtility.singleLineHeight), element,GUIContent.none); | |
} | |
public override void OnInspectorGUI() | |
{ | |
serializedObject.Update(); | |
reorderableList.DoLayoutList(); | |
serializedObject.ApplyModifiedProperties(); | |
scrollViewPos=EditorGUILayout.BeginScrollView(scrollViewPos); | |
int cantColumnas = 3; | |
float anchoCosas = Mathf.FloorToInt ((EditorGUIUtility.currentViewWidth-EditorGUIUtility.singleLineHeight*3) / cantColumnas); | |
for (int i=0; i<laLista.Count; i++) | |
{ | |
if(i%cantColumnas==0)EditorGUILayout.BeginHorizontal(); | |
Rect r = EditorGUILayout.GetControlRect(GUILayout.Width(anchoCosas),GUILayout.Height(anchoCosas)); | |
r.width -= 3; | |
r.height -= 3; | |
Sprite s = laLista[i]; | |
if (s) | |
{ | |
EditorGUI.DrawPreviewTexture(r, s.texture); | |
} | |
if (i % cantColumnas == cantColumnas-1) EditorGUILayout.EndHorizontal(); | |
} | |
if (laLista.Count%cantColumnas != 0) EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.EndScrollView(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment