mirror of
https://gitgud.io/AbstractConcept/rimworld-animation-studio.git
synced 2024-08-15 00:43:27 +00:00
Initial commit
This commit is contained in:
commit
3c7cc0c973
8391 changed files with 704313 additions and 0 deletions
|
@ -0,0 +1,283 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UnityEngine.Experimental.U2D.Animation
|
||||
{
|
||||
/// <summary>
|
||||
/// Component that holds a Sprite Library Asset. The component is used by SpriteResolver Component to query for Sprite based on Category and Index
|
||||
/// </summary>
|
||||
[DisallowMultipleComponent]
|
||||
[AddComponentMenu("2D Animation/Sprite Library (Experimental)")]
|
||||
[HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.animation@3.1/manual/SLComponent.html")]
|
||||
public class SpriteLibrary : MonoBehaviour
|
||||
{
|
||||
internal class StringAndHash
|
||||
{
|
||||
public string name;
|
||||
public int hash;
|
||||
|
||||
public StringAndHash(string name)
|
||||
{
|
||||
this.name = name;
|
||||
hash = SpriteLibraryAsset.GetStringHash(name);
|
||||
}
|
||||
|
||||
public StringAndHash(int hash)
|
||||
{
|
||||
name = "";
|
||||
this.hash = hash;
|
||||
}
|
||||
|
||||
public static bool operator==(StringAndHash l, StringAndHash r)
|
||||
{
|
||||
if (Object.ReferenceEquals(l, null) && Object.ReferenceEquals(r, null))
|
||||
return true;
|
||||
if (!Object.ReferenceEquals(l, null))
|
||||
return l.Equals(r);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool operator!=(StringAndHash l, StringAndHash r)
|
||||
{
|
||||
return !(l == r);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return this.Equals(obj as StringAndHash);
|
||||
}
|
||||
|
||||
private bool Equals(StringAndHash p)
|
||||
{
|
||||
if (Object.ReferenceEquals(p, null))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Optimization for a common success case.
|
||||
if (Object.ReferenceEquals(this, p))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// If run-time types are not exactly the same, return false.
|
||||
if (this.GetType() != p.GetType())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return (hash == p.hash) || (name == p.name);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return hash;
|
||||
}
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private SpriteLibraryAsset m_SpriteLibraryAsset;
|
||||
|
||||
Dictionary<StringAndHash, Dictionary<StringAndHash, Sprite>> m_Overrides = new Dictionary<StringAndHash, Dictionary<StringAndHash, Sprite>>();
|
||||
|
||||
/// <summary>Get or Set the current SpriteLibraryAsset to use </summary>
|
||||
public SpriteLibraryAsset spriteLibraryAsset
|
||||
{
|
||||
set
|
||||
{
|
||||
if (m_SpriteLibraryAsset != value)
|
||||
{
|
||||
m_SpriteLibraryAsset = value;
|
||||
RefreshSpriteResolvers();
|
||||
}
|
||||
}
|
||||
get { return m_SpriteLibraryAsset; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the Sprite that is registered for the given Category and Label for the SpriteLibrary
|
||||
/// </summary>
|
||||
/// <param name="category">Category name</param>
|
||||
/// <param name="label">Label name</param>
|
||||
/// <returns>Sprite associated to the name and index</returns>
|
||||
public Sprite GetSprite(string category, string label)
|
||||
{
|
||||
var categoryHash = SpriteLibraryAsset.GetStringHash(category);
|
||||
var labelHash = SpriteLibraryAsset.GetStringHash(label);
|
||||
return GetSprite(categoryHash, labelHash);
|
||||
}
|
||||
|
||||
internal Sprite GetSprite(int categoryHash, int labelHash)
|
||||
{
|
||||
return GetSprite(categoryHash, labelHash, out _);
|
||||
}
|
||||
|
||||
internal Sprite GetSprite(int categoryHash, int labelHash, out bool validEntry)
|
||||
{
|
||||
validEntry = false;
|
||||
var cat = new StringAndHash(categoryHash);
|
||||
var label = new StringAndHash(labelHash);
|
||||
if (m_Overrides.ContainsKey(cat) && m_Overrides[cat].ContainsKey(label))
|
||||
{
|
||||
validEntry = true;
|
||||
return m_Overrides[cat][label];
|
||||
}
|
||||
return m_SpriteLibraryAsset == null ? null : m_SpriteLibraryAsset.GetSprite(categoryHash, labelHash, out validEntry);
|
||||
}
|
||||
|
||||
internal string GetCategoryNameFromHash(int categoryHash)
|
||||
{
|
||||
var key = m_Overrides.Keys.FirstOrDefault(x => x.hash == categoryHash);
|
||||
if (key != null)
|
||||
return key.name;
|
||||
return m_SpriteLibraryAsset == null ? "" : m_SpriteLibraryAsset.GetCategoryNameFromHash(categoryHash);
|
||||
}
|
||||
|
||||
internal string GetLabelNameFromHash(int categoryHash, int labelHash)
|
||||
{
|
||||
var overrides = GetCategoryOverride(new StringAndHash(categoryHash), false);
|
||||
var label = overrides.Keys.FirstOrDefault(x => x.hash == labelHash);
|
||||
if (label != null)
|
||||
return label.name;
|
||||
return m_SpriteLibraryAsset == null ? "" : m_SpriteLibraryAsset.GetLabelNameFromHash(categoryHash, labelHash);
|
||||
}
|
||||
|
||||
private Dictionary<StringAndHash, Sprite> GetCategoryOverride(string category, bool addToList)
|
||||
{
|
||||
return GetCategoryOverride(new StringAndHash(category), addToList);
|
||||
}
|
||||
|
||||
private Dictionary<StringAndHash, Sprite> GetCategoryOverride(StringAndHash category, bool addToList)
|
||||
{
|
||||
Dictionary<StringAndHash, Sprite> label;
|
||||
if (m_Overrides.ContainsKey(category))
|
||||
label = m_Overrides[category];
|
||||
else
|
||||
label = new Dictionary<StringAndHash, Sprite>();
|
||||
|
||||
|
||||
if (addToList && !m_Overrides.ContainsKey(category))
|
||||
{
|
||||
if (string.IsNullOrEmpty(category.name))
|
||||
Debug.LogWarning("Adding override category with no name");
|
||||
m_Overrides.Add(category, label);
|
||||
}
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
private void AddSpriteToOverride(Dictionary<StringAndHash, Sprite> overrides, StringAndHash label, Sprite sprite)
|
||||
{
|
||||
if (overrides.ContainsKey(label))
|
||||
overrides[label] = sprite;
|
||||
else
|
||||
overrides.Add(label, sprite);
|
||||
RefreshSpriteResolvers();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add or replace an override when querying for the given Category and Label from a SpriteLibraryAsset
|
||||
/// </summary>
|
||||
/// <param name="spriteLib">Sprite Library Asset to query</param>
|
||||
/// <param name="category">Category name from the Sprite Library Asset to add override</param>
|
||||
/// <param name="label">Label name to add override</param>
|
||||
public void AddOverride(SpriteLibraryAsset spriteLib, string category, string label)
|
||||
{
|
||||
var sprite = spriteLib.GetSprite(category, label);
|
||||
var overridelabel = GetCategoryOverride(category, true);
|
||||
AddSpriteToOverride(overridelabel, new StringAndHash(label), sprite);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add or replace an override when querying for the given Category. All the categories in the Category will be added.
|
||||
/// </summary>
|
||||
/// <param name="spriteLib">Sprite Library Asset to query</param>
|
||||
/// <param name="category">Category name from the Sprite Library Asset to add override</param>
|
||||
public void AddOverride(SpriteLibraryAsset spriteLib, string category)
|
||||
{
|
||||
var categoryHash = SpriteLibraryAsset.GetStringHash(category);
|
||||
var cat = spriteLib.categories.FirstOrDefault(x => x.hash == categoryHash);
|
||||
if (cat != null)
|
||||
{
|
||||
var label = GetCategoryOverride(category, true);
|
||||
for (int i = 0; i < cat.categoryList.Count; ++i)
|
||||
{
|
||||
AddSpriteToOverride(label, new StringAndHash(cat.categoryList[i].name), cat.categoryList[i].sprite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add or replace an override when querying for the given Category and Label.
|
||||
/// </summary>
|
||||
/// <param name="sprite">Sprite to override to</param>
|
||||
/// <param name="category">Category name to override</param>
|
||||
/// <param name="label">Label name to override</param>
|
||||
public void AddOverride(Sprite sprite, string category, string label)
|
||||
{
|
||||
var overridelabel = GetCategoryOverride(category, true);
|
||||
AddSpriteToOverride(overridelabel, new StringAndHash(label), sprite);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all Sprite Library override for a given category
|
||||
/// </summary>
|
||||
/// <param name="category">Category overrides to remove</param>
|
||||
public void RemoveOverride(string category)
|
||||
{
|
||||
var hash = new StringAndHash(SpriteLibraryAsset.GetStringHash(category));
|
||||
m_Overrides.Remove(hash);
|
||||
RefreshSpriteResolvers();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove Sprite Library override for a given category and label
|
||||
/// </summary>
|
||||
/// <param name="category">Category to remove</param>
|
||||
/// <param name="label">Label to remove</param>
|
||||
public void RemoveOverride(string category, string label)
|
||||
{
|
||||
var catlabel = GetCategoryOverride(category, false);
|
||||
if (catlabel != null)
|
||||
{
|
||||
catlabel.Remove(new StringAndHash(SpriteLibraryAsset.GetStringHash(label)));
|
||||
RefreshSpriteResolvers();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Method to check if a Category and Label pair has an override
|
||||
/// </summary>
|
||||
/// <param name="category">Category name</param>
|
||||
/// <param name="label">Label name</param>
|
||||
/// <returns>True if override exist, false otherwise</returns>
|
||||
public bool HasOverride(string category, string label)
|
||||
{
|
||||
var catOverride = GetCategoryOverride(category, false);
|
||||
if (catOverride != null)
|
||||
return catOverride.ContainsKey(new StringAndHash(label));
|
||||
return false;
|
||||
}
|
||||
|
||||
internal List<SpriteLibCategory> labels
|
||||
{
|
||||
get { return m_SpriteLibraryAsset != null ? m_SpriteLibraryAsset.categories : new List<SpriteLibCategory>(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Request SpriteResolver components that are in the same hierarchy to refresh
|
||||
/// </summary>
|
||||
public void RefreshSpriteResolvers()
|
||||
{
|
||||
var spriteResolvers = GetComponentsInChildren<SpriteResolver>();
|
||||
foreach (var sr in spriteResolvers)
|
||||
{
|
||||
sr.ResolveSpriteToSpriteRenderer();
|
||||
#if UNITY_EDITOR
|
||||
sr.spriteLibChanged = true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c29cff538c195c249b69c6f2236de67b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,383 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
namespace UnityEngine.Experimental.U2D.Animation
|
||||
{
|
||||
internal interface INameHash
|
||||
{
|
||||
string name { get; set; }
|
||||
int hash { get; }
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
internal class Categorylabel : INameHash
|
||||
{
|
||||
[SerializeField]
|
||||
string m_Name;
|
||||
[SerializeField]
|
||||
[HideInInspector]
|
||||
int m_Hash;
|
||||
[SerializeField]
|
||||
Sprite m_Sprite;
|
||||
|
||||
public string name
|
||||
{
|
||||
get { return m_Name; }
|
||||
set
|
||||
{
|
||||
m_Name = value;
|
||||
m_Hash = SpriteLibraryAsset.GetStringHash(m_Name);
|
||||
}
|
||||
}
|
||||
public int hash { get { return m_Hash; } }
|
||||
public Sprite sprite {get { return m_Sprite; } set { m_Sprite = value; }}
|
||||
public void UpdateHash()
|
||||
{
|
||||
m_Hash = SpriteLibraryAsset.GetStringHash(m_Name);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
internal class SpriteLibCategory : INameHash
|
||||
{
|
||||
[SerializeField]
|
||||
string m_Name;
|
||||
[SerializeField]
|
||||
int m_Hash;
|
||||
[SerializeField]
|
||||
List<Categorylabel> m_CategoryList;
|
||||
|
||||
public string name
|
||||
{
|
||||
get { return m_Name; }
|
||||
set
|
||||
{
|
||||
m_Name = value;
|
||||
m_Hash = SpriteLibraryAsset.GetStringHash(m_Name);
|
||||
}
|
||||
}
|
||||
|
||||
public int hash { get { return m_Hash; } }
|
||||
|
||||
public List<Categorylabel> categoryList
|
||||
{
|
||||
get { return m_CategoryList; }
|
||||
set { m_CategoryList = value; }
|
||||
}
|
||||
|
||||
public void UpdateHash()
|
||||
{
|
||||
m_Hash = SpriteLibraryAsset.GetStringHash(m_Name);
|
||||
foreach (var s in m_CategoryList)
|
||||
s.UpdateHash();
|
||||
}
|
||||
|
||||
internal void ValidateLabels()
|
||||
{
|
||||
SpriteLibraryAsset.RenameDuplicate(m_CategoryList,
|
||||
(originalName, newName)
|
||||
=>
|
||||
{
|
||||
Debug.LogWarning(string.Format("Label {0} renamed to {1} due to hash clash", originalName, newName));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A custom Asset that stores Sprites grouping
|
||||
/// </summary>
|
||||
/// <Description>
|
||||
/// Sprites are grouped under a given category as categories. Each category and label needs to have
|
||||
/// a name specified so that it can be queried.
|
||||
/// </Description>
|
||||
[CreateAssetMenu(order = 350, menuName = "Sprite Library Asset (Experimental)")]
|
||||
[HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.animation@3.1/manual/SLAsset.html")]
|
||||
public class SpriteLibraryAsset : ScriptableObject
|
||||
{
|
||||
[SerializeField]
|
||||
private List<SpriteLibCategory> m_Labels = new List<SpriteLibCategory>();
|
||||
|
||||
internal List<SpriteLibCategory> categories
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_Labels;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_Labels = value;
|
||||
ValidateCategories();
|
||||
}
|
||||
}
|
||||
|
||||
internal Sprite GetSprite(int categoryHash, int labelHash)
|
||||
{
|
||||
var category = m_Labels.FirstOrDefault(x => x.hash == categoryHash);
|
||||
if (category != null)
|
||||
{
|
||||
var spritelabel = category.categoryList.FirstOrDefault(x => x.hash == labelHash);
|
||||
if (spritelabel != null)
|
||||
{
|
||||
return spritelabel.sprite;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
internal Sprite GetSprite(int categoryHash, int labelHash, out bool validEntry)
|
||||
{
|
||||
SpriteLibCategory category = null;
|
||||
for (int i = 0; i < m_Labels.Count; ++i)
|
||||
{
|
||||
if (m_Labels[i].hash == categoryHash)
|
||||
{
|
||||
category = m_Labels[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (category != null)
|
||||
{
|
||||
Categorylabel spritelabel = null;
|
||||
for (int i = 0; i < category.categoryList.Count; ++i)
|
||||
{
|
||||
if (category.categoryList[i].hash == labelHash)
|
||||
{
|
||||
spritelabel = category.categoryList[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (spritelabel != null)
|
||||
{
|
||||
validEntry = true;
|
||||
return spritelabel.sprite;
|
||||
}
|
||||
}
|
||||
validEntry = false;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the Sprite registered in the Asset given the Category and Label value
|
||||
/// </summary>
|
||||
/// <param name="category">Category string value</param>
|
||||
/// <param name="label">Label string value</param>
|
||||
/// <returns></returns>
|
||||
public Sprite GetSprite(string category, string label)
|
||||
{
|
||||
var categoryHash = SpriteLibraryAsset.GetStringHash(category);
|
||||
var labelHash = SpriteLibraryAsset.GetStringHash(label);
|
||||
return GetSprite(categoryHash, labelHash);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return all the Category names of the Sprite Library Asset that is associated.
|
||||
/// </summary>
|
||||
/// <returns>A Enumerable string value representing the name</returns>
|
||||
public IEnumerable<string> GetCategoryNames()
|
||||
{
|
||||
return m_Labels.Select(x => x.name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// (Obsolete) Returns the labels' name for the given name
|
||||
/// </summary>
|
||||
/// <param name="category">Category name</param>
|
||||
/// <returns>A Enumerable string representing labels' name</returns>
|
||||
[Obsolete("GetCategorylabelNames has been deprecated. Please use GetCategoryLabelNames (UnityUpgradable) -> GetCategoryLabelNames(*)")]
|
||||
public IEnumerable<string> GetCategorylabelNames(string category)
|
||||
{
|
||||
return GetCategoryLabelNames(category);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the labels' name for the given name
|
||||
/// </summary>
|
||||
/// <param name="category">Category name</param>
|
||||
/// <returns>A Enumerable string representing labels' name</returns>
|
||||
public IEnumerable<string> GetCategoryLabelNames(string category)
|
||||
{
|
||||
var label = m_Labels.FirstOrDefault(x => x.name == category);
|
||||
return label == null ? new string[0] : label.categoryList.Select(x => x.name);
|
||||
}
|
||||
|
||||
internal string GetCategoryNameFromHash(int hash)
|
||||
{
|
||||
var label = m_Labels.FirstOrDefault(x => x.hash == hash);
|
||||
return label == null ? "" : label.name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add or replace and existing Sprite into the given Category and Label
|
||||
/// </summary>
|
||||
/// <param name="sprite">Sprite to add</param>
|
||||
/// <param name="category">Category to add the Sprite to</param>
|
||||
/// <param name="label">Label of the Category to add the Sprite to</param>
|
||||
public void AddCategoryLabel(Sprite sprite, string category, string label)
|
||||
{
|
||||
category = category.Trim();
|
||||
label = label.Trim();
|
||||
if (string.IsNullOrEmpty(category) || string.IsNullOrEmpty(label))
|
||||
{
|
||||
Debug.LogError("Cannot add label with empty or null Category or label string");
|
||||
}
|
||||
var catHash = SpriteLibraryAsset.GetStringHash(category);
|
||||
Categorylabel categorylabel = null;
|
||||
SpriteLibCategory libCategory = null;
|
||||
libCategory = m_Labels.FirstOrDefault(x => x.hash == catHash);
|
||||
|
||||
if (libCategory != null)
|
||||
{
|
||||
Assert.AreEqual(libCategory.name, category, "Category string hash clashes with another existing Category. Please use another string");
|
||||
|
||||
var labelHash = SpriteLibraryAsset.GetStringHash(label);
|
||||
categorylabel = libCategory.categoryList.FirstOrDefault(y => y.hash == labelHash);
|
||||
if (categorylabel != null)
|
||||
{
|
||||
Assert.AreEqual(categorylabel.name, label, "Label string hash clashes with another existing label. Please use another string");
|
||||
categorylabel.sprite = sprite;
|
||||
}
|
||||
else
|
||||
{
|
||||
categorylabel = new Categorylabel()
|
||||
{
|
||||
name = label,
|
||||
sprite = sprite
|
||||
};
|
||||
libCategory.categoryList.Add(categorylabel);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var slc = new SpriteLibCategory()
|
||||
{
|
||||
categoryList = new List<Categorylabel>()
|
||||
{
|
||||
new Categorylabel()
|
||||
{
|
||||
name = label,
|
||||
sprite = sprite
|
||||
}
|
||||
},
|
||||
name = category
|
||||
};
|
||||
m_Labels.Add(slc);
|
||||
}
|
||||
#if UNITY_EDITOR
|
||||
EditorUtility.SetDirty(this);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a Label from a given Category
|
||||
/// </summary>
|
||||
/// <param name="category">Category to remove from</param>
|
||||
/// <param name="label">Label to remove</param>
|
||||
/// <param name="deleteCategory">Indicate to remove the Category if it is empty</param>
|
||||
public void RemoveCategoryLabel(string category, string label, bool deleteCategory)
|
||||
{
|
||||
var catHash = SpriteLibraryAsset.GetStringHash(category);
|
||||
SpriteLibCategory libCategory = null;
|
||||
libCategory = m_Labels.FirstOrDefault(x => x.hash == catHash);
|
||||
|
||||
if (libCategory != null)
|
||||
{
|
||||
var labelHash = SpriteLibraryAsset.GetStringHash(label);
|
||||
libCategory.categoryList.RemoveAll(x => x.hash == labelHash);
|
||||
if (deleteCategory && libCategory.categoryList.Count == 0)
|
||||
m_Labels.RemoveAll(x => x.hash == libCategory.hash);
|
||||
#if UNITY_EDITOR
|
||||
EditorUtility.SetDirty(this);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
internal string GetLabelNameFromHash(int categoryHas, int labelHash)
|
||||
{
|
||||
var labels = m_Labels.FirstOrDefault(x => x.hash == categoryHas);
|
||||
if (labels != null)
|
||||
{
|
||||
var label = labels.categoryList.FirstOrDefault(x => x.hash == labelHash);
|
||||
return label == null ? "" : label.name;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
internal void UpdateHashes()
|
||||
{
|
||||
foreach (var e in m_Labels)
|
||||
e.UpdateHash();
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorUtility.SetDirty(this);
|
||||
#endif
|
||||
}
|
||||
|
||||
internal void ValidateCategories()
|
||||
{
|
||||
RenameDuplicate(m_Labels, (originalName, newName)
|
||||
=>
|
||||
{
|
||||
Debug.LogWarning(string.Format("Category {0} renamed to {1} due to hash clash", originalName, newName));
|
||||
});
|
||||
for (int i = 0; i < m_Labels.Count; ++i)
|
||||
{
|
||||
// Verify categories have no hash clash
|
||||
var category = m_Labels[i];
|
||||
|
||||
// Verify labels have no clash
|
||||
category.ValidateLabels();
|
||||
}
|
||||
}
|
||||
|
||||
internal static void RenameDuplicate(IEnumerable<INameHash> nameHashList, Action<string, string> onRename)
|
||||
{
|
||||
const int k_IncrementMax = 1000;
|
||||
for (int i = 0; i < nameHashList.Count(); ++i)
|
||||
{
|
||||
// Verify categories have no hash clash
|
||||
var category = nameHashList.ElementAt(i);
|
||||
var categoriesClash = nameHashList.Where(x => (x.hash == category.hash || x.name == category.name) && x != category);
|
||||
int increment = 0;
|
||||
for (int j = 0; j < categoriesClash.Count(); ++j)
|
||||
{
|
||||
var categoryClash = categoriesClash.ElementAt(j);
|
||||
|
||||
while (increment < k_IncrementMax)
|
||||
{
|
||||
var name = categoryClash.name;
|
||||
name = string.Format("{0}_{1}", name, increment);
|
||||
var nameHash = SpriteLibraryAsset.GetStringHash(name);
|
||||
var exist = nameHashList.FirstOrDefault(x => (x.hash == nameHash || x.name == name) && x != categoryClash);
|
||||
if (exist == null)
|
||||
{
|
||||
onRename(categoryClash.name, name);
|
||||
categoryClash.name = name;
|
||||
break;
|
||||
}
|
||||
++increment;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Allow delegate override for test
|
||||
internal static Func<string, int> GetStringHash = Default_GetStringHash;
|
||||
internal static int Default_GetStringHash(string value)
|
||||
{
|
||||
#if DEBUG_GETSTRINGHASH_CLASH
|
||||
if (value == "abc" || value == "123")
|
||||
value = "abc";
|
||||
#endif
|
||||
var hash = Animator.StringToHash(value);
|
||||
var bytes = BitConverter.GetBytes(hash);
|
||||
var exponentialBit = BitConverter.IsLittleEndian ? 3 : 1;
|
||||
if (bytes[exponentialBit] == 0xFF)
|
||||
bytes[exponentialBit] -= 1;
|
||||
return BitConverter.ToInt32(bytes, 0);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5bf7c34a0dc4e6348bea04faa70406ee
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,201 @@
|
|||
using System;
|
||||
|
||||
namespace UnityEngine.Experimental.U2D.Animation
|
||||
{
|
||||
/// <summary>
|
||||
/// Updates a SpriteRenderer's Sprite reference on the Category and Label value it is set
|
||||
/// </summary>
|
||||
/// <Description>
|
||||
/// By setting the SpriteResolver's Category and Label value, it will request for a Sprite from
|
||||
/// a SpriteLibrary Component the Sprite that is registered for the Category and Label.
|
||||
/// If a SpriteRenderer is present in the same GameObject, the SpriteResolver will update the
|
||||
/// SpriteRenderer's Sprite reference to the corresponding Sprite.
|
||||
/// </Description>
|
||||
[ExecuteInEditMode]
|
||||
[DisallowMultipleComponent]
|
||||
[AddComponentMenu("2D Animation/Sprite Resolver (Experimental)")]
|
||||
[DefaultExecutionOrder(-2)]
|
||||
[HelpURL("https://docs.unity3d.com/Packages/com.unity.2d.animation@3.1/manual/SRComponent.html")]
|
||||
public class SpriteResolver : MonoBehaviour
|
||||
{
|
||||
// These are for animation
|
||||
[SerializeField]
|
||||
private float m_CategoryHash;
|
||||
[SerializeField]
|
||||
private float m_labelHash;
|
||||
|
||||
// For comparing hash values
|
||||
private int m_CategoryHashInt;
|
||||
private int m_LabelHashInt;
|
||||
|
||||
// For OnUpdate during animation playback
|
||||
private int m_PreviousCategoryHash;
|
||||
private int m_PreviouslabelHash;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
bool m_SpriteLibChanged;
|
||||
#endif
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
m_CategoryHashInt = ConvertFloatToInt(m_CategoryHash);
|
||||
m_PreviousCategoryHash = m_CategoryHashInt;
|
||||
m_LabelHashInt = ConvertFloatToInt(m_labelHash);
|
||||
m_PreviouslabelHash = m_LabelHashInt;
|
||||
ResolveSpriteToSpriteRenderer();
|
||||
}
|
||||
|
||||
SpriteRenderer spriteRenderer
|
||||
{
|
||||
get { return GetComponent<SpriteRenderer>(); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the Category and label to use
|
||||
/// </summary>
|
||||
/// <param name="category">Category to use</param>
|
||||
/// <param name="label">Label to use</param>
|
||||
public void SetCategoryAndLabel(string category, string label)
|
||||
{
|
||||
categoryHashInt = SpriteLibraryAsset.GetStringHash(category);
|
||||
m_PreviousCategoryHash = categoryHashInt;
|
||||
labelHashInt = SpriteLibraryAsset.GetStringHash(label);
|
||||
m_PreviouslabelHash = categoryHashInt;
|
||||
ResolveSpriteToSpriteRenderer();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the Category set for the SpriteResolver
|
||||
/// </summary>
|
||||
/// <returns>The Category's name</returns>
|
||||
public string GetCategory()
|
||||
{
|
||||
var returnString = "";
|
||||
var sl = spriteLibrary;
|
||||
if (sl)
|
||||
returnString = sl.GetCategoryNameFromHash(categoryHashInt);
|
||||
|
||||
return returnString;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the Label set for the SpriteResolver
|
||||
/// </summary>
|
||||
/// <returns>The Label's name</returns>
|
||||
public string GetLabel()
|
||||
{
|
||||
var returnString = "";
|
||||
var sl = spriteLibrary;
|
||||
if (sl)
|
||||
returnString = sl.GetLabelNameFromHash(categoryHashInt, labelHashInt);
|
||||
|
||||
return returnString;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Property to get the SpriteLibrary the SpriteResolver is resolving from
|
||||
/// </summary>
|
||||
public SpriteLibrary spriteLibrary
|
||||
{
|
||||
get
|
||||
{
|
||||
var t = transform;
|
||||
while (t != null)
|
||||
{
|
||||
var sl = t.GetComponent<SpriteLibrary>();
|
||||
if (sl != null)
|
||||
return sl;
|
||||
t = t.parent;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
m_CategoryHashInt = ConvertFloatToInt(m_CategoryHash);
|
||||
m_LabelHashInt = ConvertFloatToInt(m_labelHash);
|
||||
if (m_LabelHashInt != m_PreviouslabelHash || m_CategoryHashInt != m_PreviousCategoryHash)
|
||||
{
|
||||
m_PreviousCategoryHash = m_CategoryHashInt;
|
||||
m_PreviouslabelHash = m_LabelHashInt;
|
||||
ResolveSpriteToSpriteRenderer();
|
||||
}
|
||||
}
|
||||
|
||||
internal Sprite GetSprite(out bool validEntry)
|
||||
{
|
||||
var lib = spriteLibrary;
|
||||
if (lib != null)
|
||||
{
|
||||
return lib.GetSprite(m_CategoryHashInt, m_LabelHashInt, out validEntry);
|
||||
}
|
||||
validEntry = false;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the Sprite in SpriteResolver to the SpriteRenderer component that is in the same GameObject.
|
||||
/// </summary>
|
||||
public void ResolveSpriteToSpriteRenderer()
|
||||
{
|
||||
m_PreviousCategoryHash = m_CategoryHashInt;
|
||||
m_PreviouslabelHash = m_LabelHashInt;
|
||||
bool validEntry;
|
||||
var sprite = GetSprite(out validEntry);
|
||||
var sr = spriteRenderer;
|
||||
if (sr != null && (sprite != null || validEntry))
|
||||
sr.sprite = sprite;
|
||||
}
|
||||
|
||||
void OnTransformParentChanged()
|
||||
{
|
||||
ResolveSpriteToSpriteRenderer();
|
||||
#if UNITY_EDITOR
|
||||
spriteLibChanged = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
int categoryHashInt
|
||||
{
|
||||
get { return m_CategoryHashInt; }
|
||||
set
|
||||
{
|
||||
m_CategoryHashInt = value;
|
||||
m_CategoryHash = ConvertIntToFloat(m_CategoryHashInt);
|
||||
}
|
||||
}
|
||||
|
||||
int labelHashInt
|
||||
{
|
||||
get { return m_LabelHashInt; }
|
||||
set
|
||||
{
|
||||
m_LabelHashInt = value;
|
||||
m_labelHash = ConvertIntToFloat(m_LabelHashInt);
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe static int ConvertFloatToInt(float f)
|
||||
{
|
||||
float* fp = &f;
|
||||
int* i = (int*)fp;
|
||||
return *i;
|
||||
}
|
||||
|
||||
internal unsafe static float ConvertIntToFloat(int f)
|
||||
{
|
||||
int* fp = &f;
|
||||
float* i = (float*)fp;
|
||||
return *i;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
internal bool spriteLibChanged
|
||||
{
|
||||
get {return m_SpriteLibChanged;}
|
||||
set { m_SpriteLibChanged = value; }
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ed8b1ae4e4e52b34ea557c1c11e076fc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue