private bool UpdateProps(int scriptId) { if (harmonyPropsDirty) { // Build cached prop dictionary from child component list. HarmonyProp[] propComponents = GetComponentsInChildren <HarmonyProp>(true /*includeInactive*/); foreach (HarmonyProp propComponent in propComponents) { LoadClipName(propComponent.clipName); int propId = propComponent.CreateProp(scriptId, liveProjectFolder); if (propId >= 0) { harmonyProps[propId] = propComponent; } } harmonyPropsDirty = false; } List <int> keysToRemove = null; bool isDirty = false; foreach (KeyValuePair <int, HarmonyProp> harmonyProp in harmonyProps) { // HarmonyProp component does not exists anymore, disable prop rendering // and schedule to remove from dictionary. if (harmonyProp.Value == null) { HarmonyProp.DisableProp(scriptId, harmonyProp.Key); keysToRemove = new List <int>(); keysToRemove.Add(harmonyProp.Key); isDirty = true; } // Update HarmonyProp component. else { LoadClipName(harmonyProp.Value.clipName); bool propIsDirty = harmonyProp.Value.UpdateProp(scriptId); isDirty = isDirty || propIsDirty; } } // Clean null objects from dictionary. if (keysToRemove != null) { foreach (int key in keysToRemove) { harmonyProps.Remove(key); } } return(isDirty); }
public static void CreateOrUpdatePropsFromMetadata(GameObject rootObject) { HarmonyRenderer renderer = rootObject.GetComponent <HarmonyRenderer>(); if (renderer == null) { return; } string projectFolder = renderer.projectFolder; if (!new DirectoryInfo(projectFolder).Exists) { projectFolder = Application.streamingAssetsPath + "/" + projectFolder; } // Load props metadata from XML XML_Types.XML_PropMeta[] xmlProps = XML_StageLoader.loadPropMeta(projectFolder).ToArray(); if (xmlProps.Length > 0) { string propObjectName = "Props"; GameObject propContainerObject = null; Transform[] childTransforms = rootObject.GetComponentsInChildren <Transform>(true /*includeInactive*/); foreach (Transform childTransform in childTransforms) { GameObject childObject = childTransform.gameObject; // Direct child to root object if (childObject.transform.parent == rootObject.transform) { // Object matching preset Props game object name. if (childObject.name == propObjectName) { propContainerObject = childObject; break; } } } HarmonyProp[] propComponents = rootObject.GetComponentsInChildren <HarmonyProp>(true /*includeInactive*/); foreach (XML_Types.XML_PropMeta xmlProp in xmlProps) { if (!Array.Exists(propComponents, prop => (prop.clipName == xmlProp._clipName) && (prop.playName == xmlProp._playName))) { if (propContainerObject == null) { // Create container game object from props child to renderer object. propContainerObject = new GameObject(propObjectName); propContainerObject.transform.parent = rootObject.transform; } GameObject propObject = new GameObject(xmlProp._playName); propObject.transform.parent = propContainerObject.transform; HarmonyProp propComponent = propObject.AddComponent <HarmonyProp>(); propComponent.clipName = xmlProp._clipName; propComponent.playName = xmlProp._playName; } } } }