Created
June 19, 2014 14:35
-
-
Save rakkarage/2e3c01314d276c97f569 to your computer and use it in GitHub Desktop.
custom tk2d sprite & animation picker
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.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
namespace ca.HenrySoftware.Deko | |
{ | |
[CustomPropertyDrawer(typeof(DataAnimation))] | |
public class DataAnimationEditor : PropertyDrawer | |
{ | |
private static float _offset = EditorGUIUtility.singleLineHeight; | |
private tk2dGenericIndexItem[] _indexes = null; | |
private string[] _names = null; | |
private bool _initialized = false; | |
private void Init() | |
{ | |
if (!_initialized) | |
{ | |
_indexes = tk2dEditorUtility.GetOrCreateIndex().GetSpriteAnimations(); | |
if (_indexes != null) | |
{ | |
_names = new string[_indexes.Length]; | |
for (int i = 0; i < _indexes.Length; i++) | |
{ | |
_names[i] = _indexes[i].AssetName; | |
} | |
} | |
_initialized = true; | |
} | |
} | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
Init(); | |
if ((_indexes == null) || (_indexes.Length == 0)) | |
{ | |
if (GUI.Button(position, "None! Refresh?")) | |
{ | |
_initialized = false; | |
Init(); | |
} | |
} | |
else | |
{ | |
var collectionProperty = property.FindPropertyRelative("Collection"); | |
if (collectionProperty == null) return; | |
int selectedCollection = collectionProperty.intValue; | |
float indent = _offset * EditorGUI.indentLevel; | |
Rect region0 = position; | |
region0.width = ((position.width - _offset) * .5f) + (indent * .5f); | |
region0.height = _offset; | |
collectionProperty.intValue = EditorGUI.Popup(region0, string.Empty, selectedCollection, _names); | |
if (collectionProperty.intValue != selectedCollection) | |
{ | |
GUI.changed = true; | |
} | |
tk2dSpriteAnimation animation = _indexes[selectedCollection].GetAsset<tk2dSpriteAnimation>(); | |
if ((animation != null) && (animation.clips.Length > 0)) | |
{ | |
List<string> clipNames = new List<string>(animation.clips.Length); | |
List<int> clipIds = new List<int>(animation.clips.Length); | |
for (int i = 0; i < animation.clips.Length; i++) | |
{ | |
if (animation.clips[i].name != null && animation.clips[i].name.Length > 0) | |
{ | |
string name = animation.clips[i].name; | |
if (tk2dPreferences.inst.showIds) | |
{ | |
name += "\t[" + i.ToString() + "]"; | |
} | |
clipNames.Add(name); | |
clipIds.Add(i); | |
} | |
} | |
var indexProperty = property.FindPropertyRelative("Index"); | |
if (indexProperty == null) return; | |
int selectedIndex = ((indexProperty == null) ? 0 : indexProperty.intValue); | |
region0.x += region0.width - indent; | |
indexProperty.intValue = EditorGUI.IntPopup(region0, string.Empty, selectedIndex, clipNames.ToArray(), clipIds.ToArray()); | |
if (indexProperty.intValue != selectedIndex) | |
{ | |
GUI.changed = true; | |
} | |
if (Event.current.type == EventType.Repaint) | |
{ | |
Rect background = position; | |
background.x = _offset + indent; | |
background.y += _offset; | |
background.height = _offset * 2; | |
background.width = position.width - indent; | |
tk2dGrid.Draw(background); | |
Rect region1 = position; | |
region1.x = _offset + indent; | |
region1.y += _offset; | |
region1.width = _offset * 2; | |
region1.height = region1.width; | |
if (animation != null) | |
{ | |
tk2dSpriteAnimationClip clip = animation.clips[indexProperty.intValue]; | |
if (clip != null) | |
{ | |
foreach (var frame in clip.frames) | |
{ | |
int spriteId = frame.spriteId; | |
int first = frame.spriteCollection.FirstValidDefinitionIndex; | |
tk2dSpriteDefinition def = frame.spriteCollection.spriteDefinitions[first + spriteId]; | |
tk2dSpriteThumbnailCache.DrawSpriteTextureInRect(region1, def, Color.white); | |
region1.x += region1.width; | |
} | |
} | |
} | |
} | |
Rect region2 = position; | |
region2.x = position.width; | |
region2.width = _offset; | |
region2.height = _offset; | |
if (GUI.Button(region2, GUIContent.none)) | |
{ | |
tk2dSpriteAnimationEditorPopup v = EditorWindow.GetWindow(typeof(tk2dSpriteAnimationEditorPopup), false, "Sprite Animation Editor") as tk2dSpriteAnimationEditorPopup; | |
v.SetSpriteAnimation(animation); | |
v.Show(); | |
} | |
} | |
property.serializedObject.ApplyModifiedProperties(); | |
} | |
} | |
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | |
{ | |
return _offset * 3; | |
} | |
} | |
} |
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; | |
namespace ca.HenrySoftware.Deko | |
{ | |
[CustomPropertyDrawer(typeof(DataSprite))] | |
public class DataSpriteDrawer : PropertyDrawer | |
{ | |
private static float _offset = EditorGUIUtility.singleLineHeight; | |
private const string _collectionString = "Collection"; | |
private const string _indexString = "Index"; | |
private tk2dSpriteGuiUtility.SpriteChangedCallback _spriteChangedCallbackInstance = null; | |
private tk2dSpriteGuiUtility.SpriteChangedCallback spriteChangedCallbackInstance | |
{ | |
get | |
{ | |
if (_spriteChangedCallbackInstance == null) | |
{ | |
_spriteChangedCallbackInstance = new tk2dSpriteGuiUtility.SpriteChangedCallback(SpriteChangedCallbackImpl); | |
} | |
return _spriteChangedCallbackInstance; | |
} | |
} | |
private void SpriteChangedCallbackImpl(tk2dSpriteCollectionData spriteCollectionData, int spriteId, object data) | |
{ | |
SerializedProperty property = data as SerializedProperty; | |
var collectionProperty = property.FindPropertyRelative(_collectionString); | |
collectionProperty.objectReferenceValue = spriteCollectionData; | |
var indexProperty = property.FindPropertyRelative(_indexString); | |
indexProperty.intValue = spriteId; | |
property.serializedObject.ApplyModifiedProperties(); | |
} | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
var collectionProperty = property.FindPropertyRelative("Collection"); | |
if (collectionProperty == null) return; | |
var spriteCollectionData = collectionProperty.objectReferenceValue as tk2dSpriteCollectionData; | |
var indexProperty = property.FindPropertyRelative("Index"); | |
if (indexProperty == null) return; | |
var spriteId = indexProperty.intValue; | |
position.height = _offset; | |
tk2dSpriteGuiUtility.SpriteSelectorNew(position, spriteCollectionData, spriteId, spriteChangedCallbackInstance, property); | |
if (spriteCollectionData != null) | |
{ | |
position.x = _offset + _offset * EditorGUI.indentLevel; | |
position.y += _offset; | |
position.width = _offset * 2; | |
position.height = position.width; | |
tk2dGrid.Draw(position); | |
tk2dSpriteDefinition def = spriteCollectionData.spriteDefinitions[spriteId]; | |
tk2dSpriteThumbnailCache.DrawSpriteTextureInRect(position, def, Color.white); | |
Event ev = Event.current; | |
if (ev.type == EventType.MouseDown && ev.button == 0 && position.Contains(ev.mousePosition)) | |
{ | |
tk2dSpriteGuiUtility.SpriteSelectorPopup(spriteCollectionData, spriteId, spriteChangedCallbackInstance, property); | |
} | |
} | |
property.serializedObject.ApplyModifiedProperties(); | |
} | |
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | |
{ | |
return _offset * 3; | |
} | |
} | |
} |
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; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditorInternal; | |
namespace ca.HenrySoftware.Deko | |
{ | |
[Serializable] | |
public class DataStore : ScriptableObject | |
{ | |
public DataEffect[] Effects; | |
public DataItem[] Items; | |
public DataMob[] Mobs; | |
} | |
[Serializable] | |
public struct DataEffect | |
{ | |
public string Id; | |
public string Name; | |
public string Description; | |
public DataAttributes Attributes; | |
} | |
[Serializable] | |
public struct DataItem | |
{ | |
public string Id; | |
public string Name; | |
public string Description; | |
[Range(1, 99)] | |
public int Stack; | |
public ItemType ItemType; | |
public ItemEquipType ItemEquipType; | |
public DataAttributes Attributes; | |
public string[] EffectIds; | |
public DataSprite Sprite; | |
} | |
[Serializable] | |
public struct DataMob | |
{ | |
public string Id; | |
public string Name; | |
public string Description; | |
public string[] EffectIds; | |
public string[] ItemIds; | |
public DataAnimation[] Idles; | |
public DataAnimation[] Walks; | |
public DataAnimation[] Attacks; | |
public Color Color; | |
public string ParentMobId; | |
public string SplitMobId; | |
[Range(1, 100)] | |
public int Probability; | |
[Range(0f, 1f)] | |
public float MinDepth; | |
[Range(0f, 1f)] | |
public float MaxDepth; | |
public DataAttributes Attributes; | |
} | |
[Serializable] | |
public struct DataAttributes | |
{ | |
public Vector2 Position; | |
public int Speed; | |
public int Offense; | |
public int Defense; | |
public int Health; | |
public int HealthRegen; | |
public int HealthDrain; | |
public int Power; | |
public int PowerRegen; | |
public int PowerDrain; | |
public int Currency; | |
public int Experience; | |
public int Level; | |
public DataDamage[] Damages; | |
} | |
[Serializable] | |
public struct DataDamage | |
{ | |
public int Die; | |
public int Count; | |
public int Add; | |
public int Duration; | |
} | |
[Serializable] | |
public struct DataSprite | |
{ | |
public tk2dSpriteCollectionData Collection; | |
public int Index; | |
} | |
[Serializable] | |
public struct DataAnimation | |
{ | |
public int Collection; | |
public int Index; | |
} | |
} |
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; | |
using System.IO; | |
using UnityEditor; | |
using UnityEngine; | |
namespace ca.HenrySoftware.Deko | |
{ | |
[CustomEditor(typeof(DataStore))] | |
public class DataStoreEditor : Editor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
serializedObject.Update(); | |
EditorGUILayout.PropertyField(serializedObject.FindProperty("Effects"), true); | |
EditorGUILayout.Separator(); | |
EditorGUILayout.PropertyField(serializedObject.FindProperty("Items"), true); | |
EditorGUILayout.Separator(); | |
EditorGUILayout.PropertyField(serializedObject.FindProperty("Mobs"), true); | |
serializedObject.ApplyModifiedProperties(); | |
} | |
[MenuItem("ca.HenrySoftware.Deko/Create DataStore")] | |
public static void CreateAsset() | |
{ | |
CreateAsset<DataStore>(); | |
} | |
public static void CreateAsset<T>() where T : ScriptableObject | |
{ | |
T asset = ScriptableObject.CreateInstance<T>(); | |
string path = AssetDatabase.GetAssetPath(Selection.activeObject); | |
if (string.IsNullOrEmpty(path)) | |
{ | |
path = "Assets"; | |
} | |
else if (!string.IsNullOrEmpty(Path.GetExtension(path))) | |
{ | |
path = path.Replace(Path.GetFileName(AssetDatabase.GetAssetPath(Selection.activeObject)), string.Empty); | |
} | |
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(path + "/New " + typeof(T).ToString() + ".asset"); | |
AssetDatabase.CreateAsset(asset, assetPathAndName); | |
AssetDatabase.SaveAssets(); | |
EditorUtility.FocusProjectWindow(); | |
Selection.activeObject = asset; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment