using UnityEngine; using System.Collections.Generic; using System; namespace UIWidgets { /// /// Templates for UI. /// public class Templates where T : MonoBehaviour, ITemplatable { Dictionary templates = new Dictionary(); Dictionary> cache = new Dictionary>(); bool findTemplatesCalled; Action onCreateCallback; /// /// Initializes a new instance of the UIWidgets.Templates class. /// /// On create new UI object callback. public Templates(Action onCreateCallback = null) { this.onCreateCallback = onCreateCallback; } /// /// Finds the templates. /// public void FindTemplates() { findTemplatesCalled = true; Resources.FindObjectsOfTypeAll().ForEach(AddTemplate); } void AddTemplate(T template) { Add(template.name, template, replace: true); template.gameObject.SetActive(false); } /// /// Clears the cached instance of templates. /// public void ClearCache() { cache.Keys.ForEach(ClearCache); } /// /// Clears the cached instance of specified template. /// /// Template name. public void ClearCache(string name) { if (!cache.ContainsKey(name)) { return ; } cache[name].ForEach(DestroyCached); cache[name].Clear(); cache[name].TrimExcess(); } void DestroyCached(T cached) { if ((cached!=null) && ((cached.gameObject!=null))) { UnityEngine.Object.Destroy(cached.gameObject); } } /// /// Check if exists template with the specified name. /// /// Name. public bool Exists(string name) { return templates.ContainsKey(name); } /// /// Gets the template by name. /// /// The template. /// Template name. public T Get(string name) { if (!Exists(name)) { throw new ArgumentException("Not found template with name '" + name + "'"); } return templates[name]; } /// /// Deletes the template by name. /// /// Template name. public void Delete(string name) { if (!Exists(name)) { return ; } templates.Remove(name); ClearCache(name); } /// /// Adds the template. /// /// Template name. /// Template object. /// If set to true replace. public void Add(string name, T template, bool replace = true) { if (Exists(name)) { if (!replace) { throw new ArgumentException("Template with name '" + name + "' already exists."); } ClearCache(name); templates[name] = template; } else { templates.Add(name, template); } template.IsTemplate = true; template.TemplateName = name; } /// /// Return instance by the specified template name. /// /// Template name. public T Instance(string name) { if (!findTemplatesCalled) { FindTemplates(); } if (!Exists(name)) { throw new ArgumentException("Not found template with name '" + name + "'"); } if (templates[name]==null) { templates.Clear(); FindTemplates(); } T template; if ((cache.ContainsKey(name)) && (cache[name].Count > 0)) { template = cache[name].Pop(); } else { template = UnityEngine.Object.Instantiate(templates[name]) as T; template.TemplateName = name; template.IsTemplate = false; if (onCreateCallback!=null) { onCreateCallback(template); } } template.transform.SetParent(templates[name].transform.parent, false); return template; } /// /// Returns instance to the cache. /// /// Instance public void ToCache(T instance) { instance.gameObject.SetActive(false); if (!cache.ContainsKey(instance.TemplateName)) { cache[instance.TemplateName] = new Stack(); } cache[instance.TemplateName].Push(instance); } } }