Initial commit

This commit is contained in:
AbstractConcept 2022-09-13 00:36:34 -05:00
commit 3c7cc0c973
8391 changed files with 704313 additions and 0 deletions

View file

@ -0,0 +1,196 @@
using UnityEditor.U2D.Animation;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Experimental.U2D.Animation;
namespace UnityEditor.Experimental.U2D.Animation
{
[CustomEditor(typeof(SpriteLibraryAsset))]
internal class SpriteLibraryAssetInspector : Editor
{
static class Style
{
public static GUIContent duplicateWarningText = EditorGUIUtility.TrTextContent("Duplicate name found or name hash clashes. Please use a different name");
public static GUIContent duplicateWarning = EditorGUIUtility.TrIconContent("console.warnicon.sml", duplicateWarningText.text);
public static GUIContent nameLabel = new GUIContent(TextContent.label);
public static int lineSpacing = 3;
}
private SerializedProperty m_Labels;
private ReorderableList m_LabelReorderableList;
private bool m_UpdateHash = false;
private readonly float kElementHeight = EditorGUIUtility.singleLineHeight * 3;
public void OnEnable()
{
m_Labels = serializedObject.FindProperty("m_Labels");
m_LabelReorderableList = new ReorderableList(serializedObject, m_Labels, true, false, true, true);
SetupOrderList();
}
public void OnDisable()
{
var sla = target as SpriteLibraryAsset;
if (sla != null)
sla.UpdateHashes();
}
float GetElementHeight(int index)
{
var property = m_Labels.GetArrayElementAtIndex(index);
var spriteListProp = property.FindPropertyRelative("m_CategoryList");
if (spriteListProp.isExpanded)
return (spriteListProp.arraySize + 1) * (EditorGUIUtility.singleLineHeight + Style.lineSpacing) + kElementHeight;
return kElementHeight;
}
void DrawElement(Rect rect, int index, bool selected, bool focused)
{
var property = m_Labels.GetArrayElementAtIndex(index);
var catRect = new Rect(rect.x, rect.y, rect.width - kElementHeight, EditorGUIUtility.singleLineHeight);
var vaRect = new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight, rect.width - kElementHeight, EditorGUIUtility.singleLineHeight);
var categoryProp = property.FindPropertyRelative("m_Name");
var spriteListProp = property.FindPropertyRelative("m_CategoryList");
EditorGUI.BeginChangeCheck();
var newCatName = EditorGUI.DelayedTextField(catRect, categoryProp.stringValue);
if (EditorGUI.EndChangeCheck())
{
newCatName = newCatName.Trim();
m_UpdateHash = true;
if (categoryProp.stringValue != newCatName)
{
// Check if this nameLabel is already taken
if (!IsNameInUsed(newCatName, m_Labels, "m_Name", 0))
categoryProp.stringValue = newCatName;
else
Debug.LogWarning(Style.duplicateWarningText.text);
}
}
EditorGUI.PropertyField(vaRect, spriteListProp);
if (spriteListProp.isExpanded)
{
EditorGUI.indentLevel++;
var indentedRect = EditorGUI.IndentedRect(vaRect);
var labelWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 40 + indentedRect.x - vaRect.x;
indentedRect.y += EditorGUIUtility.singleLineHeight + Style.lineSpacing;
var sizeRect = indentedRect;
int size = EditorGUI.IntField(sizeRect, TextContent.size, spriteListProp.arraySize);
if (size != spriteListProp.arraySize && size >= 0)
spriteListProp.arraySize = size;
indentedRect.y += EditorGUIUtility.singleLineHeight + Style.lineSpacing;
DrawSpriteListProperty(indentedRect, spriteListProp);
EditorGUIUtility.labelWidth = labelWidth;
EditorGUI.indentLevel--;
}
}
void DrawSpriteListProperty(Rect rect, SerializedProperty spriteListProp)
{
for (int i = 0; i < spriteListProp.arraySize; ++i)
{
var element = spriteListProp.GetArrayElementAtIndex(i);
EditorGUI.BeginChangeCheck();
var oldName = element.FindPropertyRelative("m_Name").stringValue;
var nameRect = new Rect(rect.x, rect.y, rect.width / 2, EditorGUIUtility.singleLineHeight);
bool nameDuplicate = IsNameInUsed(oldName, spriteListProp, "m_Name", 1);
if (nameDuplicate)
{
nameRect.width -= 20;
}
var newName = EditorGUI.DelayedTextField(
nameRect,
Style.nameLabel,
oldName);
if (nameDuplicate)
{
nameRect.x += nameRect.width;
nameRect.width = 20;
GUI.Label(nameRect, Style.duplicateWarning);
}
if (EditorGUI.EndChangeCheck())
{
newName = newName.Trim();
element.FindPropertyRelative("m_Name").stringValue = newName;
}
EditorGUI.PropertyField(new Rect(rect.x + rect.width / 2 + 5, rect.y, rect.width / 2, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("m_Sprite"));
rect.y += EditorGUIUtility.singleLineHeight + Style.lineSpacing;
}
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUI.BeginChangeCheck();
if (EditorGUI.EndChangeCheck())
SetupOrderList();
m_UpdateHash = false;
m_LabelReorderableList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
if (m_UpdateHash)
(target as SpriteLibraryAsset).UpdateHashes();
}
bool IsNameInUsed(string name, SerializedProperty property, string propertyField, int threshold)
{
int count = 0;
var nameHash = SpriteLibraryAsset.GetStringHash(name);
for (int i = 0; i < property.arraySize; ++i)
{
var sp = property.GetArrayElementAtIndex(i);
var otherName = sp.FindPropertyRelative(propertyField).stringValue;
var otherNameHash = SpriteLibraryAsset.GetStringHash(otherName);
if (otherName == name || nameHash == otherNameHash)
{
count++;
if (count > threshold)
return true;
}
}
return false;
}
void OnAddCallback(ReorderableList list)
{
var oldSize = m_Labels.arraySize;
m_Labels.arraySize += 1;
const string kNewCatName = "New Category";
string newCatName = kNewCatName;
int catNameIncrement = 1;
while (true)
{
if (IsNameInUsed(newCatName, m_Labels, "m_Name", 0))
newCatName = string.Format("{0} {1}", kNewCatName, catNameIncrement++);
else
break;
}
var sp = m_Labels.GetArrayElementAtIndex(oldSize);
sp.FindPropertyRelative("m_Name").stringValue = newCatName;
sp.FindPropertyRelative("m_Hash").intValue = SpriteLibraryAsset.GetStringHash(newCatName);
}
private void SetupOrderList()
{
m_LabelReorderableList.drawElementCallback = DrawElement;
m_LabelReorderableList.elementHeight = kElementHeight;
m_LabelReorderableList.elementHeightCallback = GetElementHeight;
m_LabelReorderableList.onAddCallback = OnAddCallback;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 599f7dd3d2f02c04093072e31f7f7069
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,222 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.U2D.Animation;
using UnityEngine.Serialization;
namespace UnityEditor.U2D.Animation
{
/// <summary>
/// Structure that defines a Sprite Library Category Label
/// </summary>
[Serializable]
public struct SpriteCategoryLabel
{
[SerializeField]
string m_Name;
[SerializeField]
string m_SpriteId;
/// <summary>
/// Get and set the name for the Sprite label
/// </summary>
public string name
{
get { return m_Name; }
set { m_Name = value; }
}
/// <summary>
/// Get and set the Sprite Id.
/// </summary>
public string spriteId
{
get { return m_SpriteId; }
set { m_SpriteId = value; }
}
}
/// <summary>
/// Structure that defines a Sprite Library Category.
/// </summary>
[Serializable]
public struct SpriteCategory
{
[SerializeField]
[FormerlySerializedAs("name")]
string m_Name;
[SerializeField]
List<SpriteCategoryLabel> m_Labels;
/// <summary>
/// Get and set the name for the Sprite Category
/// </summary>
public string name
{
get { return m_Name; }
set { m_Name = value; }
}
/// <summary>
/// Get and set the Sprites registered to this category.
/// </summary>
public List<SpriteCategoryLabel> labels
{
get { return m_Labels; }
set { m_Labels = value; }
}
}
/// <summary>
/// A structure to hold a collection of SpriteCategory
/// </summary>
[Serializable]
public struct SpriteCategoryList
{
[SerializeField]
[FormerlySerializedAs("categories")]
List<SpriteCategory> m_Categories;
/// <summary>
/// Get or set the a list of SpriteCategory
/// </summary>
public List<SpriteCategory> categories
{
get { return m_Categories; }
set { m_Categories = value; }
}
}
internal class SpriteCategoryListCacheObject : SkinningObject
{
[SerializeField]
public List<SpriteCategory> categories = new List<SpriteCategory>();
public void CopyFrom(SpriteCategoryList categoryList)
{
categories.Clear();
foreach (var cat in categoryList.categories)
{
var spriteLibCategory = new SpriteCategory()
{
name = cat.name,
labels = new List<SpriteCategoryLabel>(cat.labels)
};
categories.Add(spriteLibCategory);
}
}
public SpriteCategoryList ToSpriteLibrary()
{
var spriteLibrary = new SpriteCategoryList();
spriteLibrary.categories = new List<SpriteCategory>();
foreach (var cat in categories)
{
var spriteLibCategory = new SpriteCategory()
{
name = cat.name,
labels = new List<SpriteCategoryLabel>(cat.labels)
};
spriteLibrary.categories.Add(spriteLibCategory);
}
return spriteLibrary;
}
public void RemoveSpriteFromCategory(string sprite)
{
for (int i = 0; i < categories.Count; ++i)
{
var index = categories[i].labels.FindIndex(x => x.spriteId == sprite);
if (index != -1)
categories[i].labels.RemoveAt(index);
}
}
public void AddSpriteToCategory(string category, SpriteCategoryLabel label)
{
if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(label.name))
{
// Remove sprite from name
RemoveSpriteFromCategory(label.spriteId);
}
else
{
//find cateogry
var categoryIndex = categories.FindIndex(x => x.name == category);
if (categoryIndex == -1)
{
// check if the hash might clash
var hash = SpriteLibraryAsset.GetStringHash(category);
if (categories.FindIndex(x => x.name != category && SpriteLibraryAsset.GetStringHash(x.name) == hash) != -1)
{
Debug.LogError("Unable to add Sprite to new Category due to name hash clash");
return;
}
}
var insertCategory = categoryIndex != -1 ? categories[categoryIndex] : new SpriteCategory() { name = category, labels = new List<SpriteCategoryLabel>() };
if (insertCategory.labels.FindIndex(x => x.spriteId == label.spriteId) == -1)
insertCategory.labels.Add(label);
// now remove everything that has this sprite
foreach (var cat in categories)
{
if (cat.name != category)
cat.labels.RemoveAll(x => x.spriteId == label.spriteId);
}
if (categoryIndex == -1)
categories.Add(insertCategory);
else
categories[categoryIndex] = insertCategory;
}
}
public void ChangeSpriteLabelName(string labelname, string sprite)
{
// find name which contain sprite
var categoryIndex = -1;
var spriteIndex = -1;
for (int i = 0; i < categories.Count; ++i)
{
spriteIndex = categories[i].labels.FindIndex(x => x.spriteId == sprite);
if (spriteIndex != -1)
{
categoryIndex = i;
break;
}
}
if (categoryIndex != -1 && spriteIndex != -1)
{
var cat = categories[categoryIndex];
if (string.IsNullOrEmpty(labelname))
{
cat.labels.RemoveAt(spriteIndex);
}
else
{
var label = cat.labels[spriteIndex];
label.name = labelname;
cat.labels[spriteIndex] = label;
}
}
}
}
/// <summary>An interface that allows Sprite Editor Modules to edit Sprite Library data for user custom importer.</summary>
/// <remarks>Implement this interface for [[ScriptedImporter]] to leverage on Sprite Editor Modules to edit Sprite Library data.</remarks>
public interface ISpriteLibDataProvider
{
/// <summary>
/// Returns the SpriteCategoryList structure that represents the Sprite Library data.
/// </summary>
/// <returns>SpriteCategoryList data</returns>
SpriteCategoryList GetSpriteCategoryList();
/// <summary>
/// Sets the SpriteCategoryList structure that represents the Sprite Library data to the data provider
/// </summary>
/// <param name="spriteCategoryList">Data to set</param>
void SetSpriteCategoryList(SpriteCategoryList spriteCategoryList);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 37848a25a68e0924ea2814f224e4d44a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,38 @@
using UnityEngine.Experimental.U2D.Animation;
namespace UnityEditor.Experimental.U2D.Animation
{
[CustomEditor(typeof(SpriteLibrary))]
[CanEditMultipleObjects]
internal class SpriteLibraryInspector : Editor
{
private SerializedProperty m_SpriteLib;
public void OnEnable()
{
m_SpriteLib = serializedObject.FindProperty("m_SpriteLibraryAsset");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_SpriteLib);
if (EditorGUI.EndChangeCheck())
{
serializedObject.ApplyModifiedProperties();
foreach (var t in targets)
{
var srs = (t as SpriteLibrary).GetComponentsInChildren<SpriteResolver>();
foreach (var sr in srs)
{
sr.ResolveSpriteToSpriteRenderer();
sr.spriteLibChanged = true;
}
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 02625be83abde4b498b0acf0df6237ff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,206 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.U2D.Animation;
using UnityEngine;
using UnityEngine.Experimental.U2D.Animation;
using UnityEngine.U2D.Animation;
namespace UnityEditor.Experimental.U2D.Animation
{
[CustomEditor(typeof(SpriteResolver))]
internal class SpriteResolverInspector : Editor
{
static class Style
{
public static GUIContent noSpriteLibContainer = EditorGUIUtility.TrTextContent("No Sprite Library Container Component found or Sprite Library has no categories.");
public static GUIContent categoryLabel = EditorGUIUtility.TrTextContent("Category");
public static GUIContent labelLabel = EditorGUIUtility.TrTextContent("Label");
public static GUIContent categoryIsEmptyLabel = EditorGUIUtility.TrTextContent("Category is Empty");
}
struct SpriteCategorySelectionList
{
public string categoryName;
public int categoryNameHash;
public string[] names;
public int[] nameHash;
public Sprite[] sprites;
}
private SerializedProperty m_SpriteCategoryHash;
private SerializedProperty m_SpritelabelHash;
private SpriteSkin m_SpriteSkin;
Dictionary<int, SpriteCategorySelectionList> m_SpriteLibSelection = new Dictionary<int, SpriteCategorySelectionList>();
string[] m_CategorySelection;
int[] m_CategorySelectionHash;
int m_CategorySelectionIndex = 0;
int m_PreviousCategoryHash = 0;
int m_labelSelectionIndex = 0;
int m_PreviouslabelHash = 0;
SpriteSelectorWidget m_SpriteSelectorWidget = new SpriteSelectorWidget();
public void OnEnable()
{
m_SpriteCategoryHash = serializedObject.FindProperty("m_CategoryHash");
m_SpritelabelHash = serializedObject.FindProperty("m_labelHash");
m_SpriteSkin = (target as SpriteResolver).GetComponent<SpriteSkin>();
m_PreviousCategoryHash = SpriteResolver.ConvertFloatToInt(m_SpriteCategoryHash.floatValue);
m_PreviouslabelHash = SpriteResolver.ConvertFloatToInt(m_SpritelabelHash.floatValue);
UpdateSpriteLibrary();
}
SpriteResolver spriteResolver { get {return target as SpriteResolver; } }
void UpdateSpriteLibrary()
{
m_SpriteLibSelection.Clear();
int categoryHash = SpriteResolver.ConvertFloatToInt(m_SpriteCategoryHash.floatValue);
int labelHash = SpriteResolver.ConvertFloatToInt(m_SpritelabelHash.floatValue);
var spriteLib = spriteResolver.spriteLibrary;
if (spriteLib != null)
{
foreach (var labels in spriteLib.labels)
{
if (!m_SpriteLibSelection.ContainsKey(labels.hash))
{
var nameHash = labels.categoryList.Select(x => x.hash).Distinct().ToArray();
if (nameHash.Length > 0)
{
var selectionList = new SpriteCategorySelectionList()
{
names = nameHash.Select(x =>
{
var v = labels.categoryList.FirstOrDefault(y => y.hash == x);
return v.name;
}).ToArray(),
nameHash = nameHash,
sprites = nameHash.Select(x =>
{
var v = labels.categoryList.FirstOrDefault(y => y.hash == x);
return v.sprite;
}).ToArray(),
categoryName = labels.name,
categoryNameHash = labels.hash
};
m_SpriteLibSelection.Add(labels.hash, selectionList);
}
}
}
}
m_CategorySelection = new string[1 + m_SpriteLibSelection.Keys.Count];
m_CategorySelection[0] = TextContent.none;
m_CategorySelectionHash = new int[1 + m_SpriteLibSelection.Keys.Count];
m_CategorySelectionHash[0] = SpriteLibraryAsset.GetStringHash(TextContent.none);
for (int i = 0; i < m_SpriteLibSelection.Keys.Count; ++i)
{
var selection = m_SpriteLibSelection[m_SpriteLibSelection.Keys.ElementAt(i)];
m_CategorySelection[i + 1] = selection.categoryName;
m_CategorySelectionHash[i + 1] = selection.categoryNameHash;
if (selection.categoryNameHash == categoryHash)
m_CategorySelectionIndex = i + 1;
}
ValidateCategorySelectionIndexValue();
if (m_CategorySelectionIndex > 0)
{
m_SpriteSelectorWidget.UpdateContents(m_SpriteLibSelection[m_CategorySelectionHash[m_CategorySelectionIndex]].sprites);
if (m_SpriteLibSelection.ContainsKey(categoryHash))
{
m_labelSelectionIndex = Array.FindIndex(m_SpriteLibSelection[categoryHash].nameHash, x => x == labelHash);
}
}
spriteResolver.spriteLibChanged = false;
}
void ValidateCategorySelectionIndexValue()
{
if (m_CategorySelectionIndex < 0 || m_CategorySelectionHash.Length <= m_CategorySelectionIndex)
m_CategorySelectionIndex = 0;
}
public override void OnInspectorGUI()
{
serializedObject.Update();
if (spriteResolver.spriteLibChanged)
UpdateSpriteLibrary();
var currentlabelHashValue = SpriteResolver.ConvertFloatToInt(m_SpritelabelHash.floatValue);
var currentCategoryHashValue = SpriteResolver.ConvertFloatToInt(m_SpriteCategoryHash.floatValue);
m_CategorySelectionIndex = Array.FindIndex(m_CategorySelectionHash, x => x == currentCategoryHashValue);
ValidateCategorySelectionIndexValue();
if (m_CategorySelection.Length == 1)
{
EditorGUILayout.LabelField(Style.noSpriteLibContainer);
}
else
{
EditorGUI.BeginChangeCheck();
m_CategorySelectionIndex = EditorGUILayout.Popup(Style.categoryLabel, m_CategorySelectionIndex, m_CategorySelection);
if (m_CategorySelectionIndex != 0)
{
var selection = m_SpriteLibSelection[m_CategorySelectionHash[m_CategorySelectionIndex]];
if (selection.names.Length <= 0)
{
EditorGUILayout.LabelField(Style.categoryIsEmptyLabel);
}
else
{
if (m_labelSelectionIndex < 0 || m_labelSelectionIndex >= selection.names.Length)
m_labelSelectionIndex = 0;
m_labelSelectionIndex = EditorGUILayout.Popup(Style.labelLabel, m_labelSelectionIndex, selection.names);
m_labelSelectionIndex = m_SpriteSelectorWidget.ShowGUI(m_labelSelectionIndex);
}
}
if (EditorGUI.EndChangeCheck())
{
currentCategoryHashValue = m_CategorySelectionHash[m_CategorySelectionIndex];
if (m_SpriteLibSelection.ContainsKey(currentCategoryHashValue))
{
var hash = m_SpriteLibSelection[currentCategoryHashValue].nameHash;
if (hash.Length > 0)
{
if (m_labelSelectionIndex < 0 || m_labelSelectionIndex >= hash.Length)
m_labelSelectionIndex = 0;
currentlabelHashValue = m_SpriteLibSelection[currentCategoryHashValue].nameHash[m_labelSelectionIndex];
}
}
m_SpriteCategoryHash.floatValue = SpriteResolver.ConvertIntToFloat(currentCategoryHashValue);
m_SpritelabelHash.floatValue = SpriteResolver.ConvertIntToFloat(currentlabelHashValue);
serializedObject.ApplyModifiedProperties();
var sf = target as SpriteResolver;
if (m_SpriteSkin != null)
m_SpriteSkin.ignoreNextSpriteChange = true;
sf.ResolveSpriteToSpriteRenderer();
}
if (m_PreviousCategoryHash != currentCategoryHashValue)
{
if (m_SpriteLibSelection.ContainsKey(currentCategoryHashValue))
{
m_SpriteSelectorWidget.UpdateContents(m_SpriteLibSelection[currentCategoryHashValue].sprites);
}
m_PreviousCategoryHash = currentCategoryHashValue;
}
if (m_PreviouslabelHash != currentlabelHashValue)
{
if (m_SpriteLibSelection.ContainsKey(currentCategoryHashValue))
m_labelSelectionIndex = Array.FindIndex(m_SpriteLibSelection[currentCategoryHashValue].nameHash, x => x == currentlabelHashValue);
m_PreviouslabelHash = currentlabelHashValue;
}
serializedObject.ApplyModifiedProperties();
if (m_SpriteSelectorWidget.NeedUpdatePreview())
this.Repaint();
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fcf9d5fa95289914b82e16091eedfb4f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,120 @@
using System;
using UnityEngine;
using System.Collections.Generic;
namespace UnityEditor.Experimental.U2D.Animation
{
internal class SpriteSelectorWidget
{
class Styles
{
public GUIStyle gridListStyle;
public Styles()
{
gridListStyle = new GUIStyle("GridList");
gridListStyle.alignment = GUI.skin.button.alignment;
}
}
Sprite[] m_SpriteList = null;
Texture2D[] m_SpritePreviews = null;
List<int> m_SpritePreviewNeedFetching = new List<int>();
Vector2 m_ScrollPos;
Styles m_Style;
const int kTargetPreviewSize = 64;
public SpriteSelectorWidget()
{}
public void UpdateContents(Sprite[] sprites)
{
m_SpriteList = sprites;
m_SpritePreviews = new Texture2D[sprites.Length];
for (int i = 0; i < m_SpritePreviews.Length; ++i)
m_SpritePreviewNeedFetching.Add(i);
UpdateSpritePreviews();
}
public int ShowGUI(int selectedIndex)
{
if (m_Style == null)
m_Style = new Styles();
UpdateSpritePreviews();
if (m_SpriteList == null || m_SpriteList.Length == 0)
return selectedIndex;
selectedIndex = (selectedIndex > m_SpriteList.Length) ? 0 : selectedIndex;
using (var topRect = new EditorGUILayout.HorizontalScope())
{
//GUILayout.Label(Styles.spriteList, EditorStyles.label, new [] {GUILayout.Width(EditorGUIUtility.labelWidth - 5)});
using (var selectionGridRect = new EditorGUILayout.HorizontalScope("box", new[] {GUILayout.ExpandWidth(true)}))
{
{
float columnF;
int columnCount, rowCount;
GetRowColumnCount(EditorGUIUtility.currentViewWidth, kTargetPreviewSize, m_SpriteList.Length, out columnCount, out rowCount, out columnF);
if (columnCount > 0 && rowCount > 0)
{
float contentSize = (columnF * kTargetPreviewSize) / columnCount;
if (rowCount >= 2)
m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos, GUIStyle.none, GUI.skin.verticalScrollbar, new[] { GUILayout.Height(rowCount > 1 ? contentSize * 2 : contentSize) });
m_Style.gridListStyle.fixedWidth = contentSize;
m_Style.gridListStyle.fixedHeight = contentSize;
selectedIndex = ContentSelectionGrid(selectedIndex, m_SpriteList, m_Style.gridListStyle, columnCount - 1);
if (rowCount >= 2)
EditorGUILayout.EndScrollView();
}
}
}
}
return selectedIndex;
}
static void GetRowColumnCount(float drawWidth, int size, int contentCount, out int column, out int row, out float columnf)
{
columnf = (drawWidth) / size;
column = (int)columnf;
if (column == 0)
row = 0;
else
row = (int)Mathf.Ceil((contentCount + column - 1) / column);
}
int ContentSelectionGrid(int selected, Sprite[] contents, GUIStyle style, int columnCount)
{
if (contents != null && contents.Length != 0)
{
selected = GUILayout.SelectionGrid(selected, m_SpritePreviews, columnCount, style);
}
return selected;
}
public bool NeedUpdatePreview()
{
return m_SpritePreviewNeedFetching.Count > 0;
}
void UpdateSpritePreviews()
{
for (int i = 0; i < m_SpritePreviewNeedFetching.Count; ++i)
{
var index = m_SpritePreviewNeedFetching[i];
if(m_SpriteList[index] == null)
m_SpritePreviews[index] = EditorGUIUtility.Load("icons/console.erroricon.png") as Texture2D;
else
m_SpritePreviews[index] = AssetPreview.GetAssetPreview(m_SpriteList[index]);
if (m_SpritePreviews[index] != null)
{
m_SpritePreviewNeedFetching.RemoveAt(i);
--i;
}
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4c818720e20ebad4b9d7ba3cc548cdb6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: