/// <summary>Initialize template.</summary> protected virtual void Start() { // check template #if DEBUG string message; if (!YouTubeThumbnailContainer.HasValidTemplate(this, out message)) { Debug.LogError("[mod.io] " + message, this); return; } #endif // get template vars Transform templateParent = this.containerTemplate.parent; string templateInstance_name = this.containerTemplate.gameObject.name + " (Instance)"; int templateInstance_index = this.containerTemplate.GetSiblingIndex() + 1; this.m_itemTemplate = this.containerTemplate.GetComponentInChildren <YouTubeThumbnailDisplay>(true); // duplication protection bool isInstantiated = (templateParent.childCount > templateInstance_index && templateParent.GetChild(templateInstance_index).gameObject.name == templateInstance_name); if (isInstantiated) { this.m_templateClone = templateParent.GetChild(templateInstance_index).gameObject; YouTubeThumbnailDisplay[] itemInstances = this.m_templateClone.GetComponentsInChildren <YouTubeThumbnailDisplay>(true); if (itemInstances == null || itemInstances.Length == 0) { isInstantiated = false; GameObject.Destroy(this.m_templateClone); } else { this.m_container = (RectTransform)itemInstances[0].transform.parent; foreach (YouTubeThumbnailDisplay item in itemInstances) { GameObject.Destroy(item.gameObject); } } } if (!isInstantiated) { this.m_templateClone = GameObject.Instantiate(this.containerTemplate.gameObject, templateParent); this.m_templateClone.transform.SetSiblingIndex(templateInstance_index); this.m_templateClone.name = templateInstance_name; YouTubeThumbnailDisplay itemInstance = this.m_templateClone.GetComponentInChildren <YouTubeThumbnailDisplay>(true); this.m_container = (RectTransform)itemInstance.transform.parent; GameObject.Destroy(itemInstance.gameObject); this.m_templateClone.SetActive(true); } this.DisplayThumbnails(this.m_modId, this.m_youTubeIds); }
/// <summary>Copies the data of another component to display.</summary> public void DisplayYouTubeThumbnail(YouTubeThumbnailDisplay display) { int modId = ModProfile.NULL_ID; string youTubeId = null; if (display != null) { modId = display.ModId; youTubeId = display.YouTubeId; } this.DisplayYouTubeThumbnail(modId, youTubeId); }
// ---------[ UTILITY ]--------- /// <summary>Checks a YouTubeThumbnailContainer's template structure.</summary> public static bool HasValidTemplate(YouTubeThumbnailContainer container, out string helpMessage) { helpMessage = null; bool isValid = true; YouTubeThumbnailDisplay itemTemplate = null; // null check if (container.containerTemplate == null) { helpMessage = ("Invalid template:" + " The container template is unassigned."); isValid = false; } // containerTemplate is child of Component else if (!container.containerTemplate.IsChildOf(container.transform) || container.containerTemplate == container.transform) { helpMessage = ("Invalid template:" + " The container template must be a child of this object."); isValid = false; } // YouTubeThumbnailDisplay is found under containerTemplate else if ((itemTemplate = container.containerTemplate.gameObject.GetComponentInChildren <YouTubeThumbnailDisplay>()) == null) { helpMessage = ("Invalid template:" + " No YouTubeThumbnailDisplay component found in the children of the container template."); isValid = false; } // YouTubeThumbnailDisplay is on same gameObject as containerTemplate else if (itemTemplate.transform == container.containerTemplate) { helpMessage = ("Invalid template:" + " The YouTubeThumbnailDisplay component cannot share a GameObject with the container template."); isValid = false; } return(isValid); }