public SkinnedMeshRenderer GetSkinMeshRecursive(Transform t) { Transform tempTransform; SkinnedMeshRenderer smr = null; var queue = new Queue <Transform>(); queue.Enqueue(t); while (queue.Count > 0) { // Take the next node from the front of the queue var node = queue.Dequeue(); // Process the node 'node' smr = node.GetComponent <SkinnedMeshRenderer>(); if (!(smr == null || smr.ToString() == "null")) { return(smr); } // Add the node’s children to the back of the queue int childCount = node.childCount; for (int i = 0; i < childCount; i++) { tempTransform = node.GetChild(i); queue.Enqueue(tempTransform); } } // None of the nodes matched the specified predicate. return(null); }
/// <summary> /// Loads the person mesh renderer. /// </summary> private void LoadPersonMeshRenderer() { Helper helper = new Helper(); SkinnedMeshRenderer mr = helper.GetSkinMeshRecursive(transform); CharacterWearableController wearableController = transform.GetComponent <CharacterWearableController>(); if (mr != null && mr.ToString() != "null") { SetPersonMeshRenderer(mr); } }
private static void SkinnedMeshRendererGui() { if (GUILayout.Button((showSMRGui ? "Hide" : "Show") + " SkinnedMeshRenderer controls")) { showSMRGui = !showSMRGui; } if (showSMRGui) { SkinnedMeshRenderer[] skinnedMeshRenderers = CurrentPart.internalModel.GetComponentsInChildren <SkinnedMeshRenderer>(); if (skinnedMeshRenderers.Length == 0) { GUILayout.Label("No SkinnedMeshRenderers"); return; } GuiUtils.label("SkinnedMeshRenderers", skinnedMeshRenderers.Length); if (_skinnedMeshRendererIndex >= skinnedMeshRenderers.Length) { _skinnedMeshRendererIndex = 0; } if (_skinnedMeshRendererIndex < 0) { _skinnedMeshRendererIndex = skinnedMeshRenderers.Length - 1; } GuiUtils.label("Current SkinnedMeshRenderer", _skinnedMeshRendererIndex + 1); SkinnedMeshRenderer smr = skinnedMeshRenderers[_skinnedMeshRendererIndex]; GUILayout.BeginHorizontal(); if (GUILayout.Button("Previous SkinnedMeshRenderer")) { _skinnedMeshRendererIndex--; } if (GUILayout.Button("Next SkinnedMeshRenderer")) { _skinnedMeshRendererIndex++; } GUILayout.EndHorizontal(); if (smr.material != null) { smr.enabled = GUILayout.Toggle(smr.enabled, "Enabled"); } else { GUILayout.Label("Material is null"); } /*if (GUILayout.Button("Hide all SMRs but this")) * { * for (int i = 0; i < skinnedMeshRenderers.Length; i++) * { * skinnedMeshRenderers[skinnedMeshRendererIndex].enabled = i != skinnedMeshRendererIndex; * } * }*/ GUILayout.Label("<b>SkinnedMeshRenderer</b>"); GUILayout.Label(smr == null ? "null" : smr.ToString()); GUILayout.Label("<b>Material</b>"); GUILayout.Label(smr.material == null ? "null" : smr.material.ToString()); GUILayout.Label("<b>Transform</b>"); GUILayout.Label(smr.transform == null ? "null" : smr.transform.ToString()); //GUILayout.Label("SkinnedMeshRenderer"); //GuiUtils.label("SkinnedMeshRenderer", smr); //GuiUtils.label("Material", smr.material); //GuiUtils.label("Transform", smr.transform); //if (smr.transform != null) { GuiUtils.label("Transform position", smr.transform.position); GuiUtils.label("Range", Vector3.Distance(smr.transform.position, InternalCamera.Instance.transform.position)); } } }
/// <summary> /// Applies the wearable on the character. /// </summary> /// <param name="wearableType">Type of wearable.</param> /// <param name="wearableName">Wearable Name</param> /// <param name="editorMode">Is Editor Mode or not.</param> public void ApplyWearable(WearableType wearableType, string wearableName, bool editorMode) { //Check if the person mesh renderer is initialized or not. //If not initialized then lazy load it. if (_personMeshRenderer == null || _personMeshRenderer.ToString() == "null" || _personBoneMap == null) { LoadPersonMeshRenderer(); } if (_wearables != null && _wearables.Exists(w => w.Name == wearableName)) { Wearable wearable = _wearables.Find(w => w.Name == wearableName); if (wearable != null) { //Load the wearable gameobject first and cache it. GameObject g = Resources.Load(string.Format("Characters/{0}/Wearables/{1}", wearable.CharacterFolderName, wearable.Name), typeof(GameObject)) as GameObject; if (g != null) { g = Instantiate(g); g.transform.parent = transform; g.transform.localPosition = new Vector3(0, 0, 0); g.transform.localRotation = Quaternion.identity; //Set the wearable mesh renderer Helper helper = new Helper(); _wearableMeshRenderer = helper.GetSkinMeshRecursive(g.transform); g.SetActive(false); } //Remove the existing wearable RemoveWearable(wearableType, editorMode); //Apply the new wearable if (_wearableMeshRenderer != null) { g.SetActive(true); Transform[] newBones = new Transform[_wearableMeshRenderer.bones.Length]; for (int i = 0; i < _wearableMeshRenderer.bones.Length; ++i) { GameObject bone = _wearableMeshRenderer.bones[i].gameObject; if (_personBoneMap.ContainsKey(bone.name)) { newBones[i] = _personBoneMap[bone.name]; } else { newBones[i] = _wearableMeshRenderer.bones[i]; } } _wearableMeshRenderer.bones = newBones; wearable.AttachGameObject(g); } else { if (editorMode) { DestroyImmediate(g); } else { Destroy(g); } } } } }