/// <summary> /// Returns the linked ItemVisuals for the provided VisualPrefabType (if any), otherwise null. /// </summary> /// <param name="type">The type of Visual Prefab you want.</param> /// <returns>The linked Transform, or null.</returns> public Transform GetVisuals(VisualPrefabType type) { switch (type) { case VisualPrefabType.VisualPrefab: return(ItemVisuals); case VisualPrefabType.SpecialVisualPrefabDefault: return(ItemSpecialVisuals); case VisualPrefabType.SpecialVisualPrefabFemale: return(ItemSpecialFemaleVisuals); default: return(null); } }
/// <summary> Clone's an items current visual prefab (and materials), then sets this item's visuals to the new cloned prefab. </summary> public static GameObject CloneVisualPrefab(Item item, VisualPrefabType type, bool logging = false) { var prefab = GetOrigItemVisuals(item, type); if (!prefab) { if (logging) { SL.Log("Error, no VisualPrefabType defined or could not find visual prefab of that type!"); } return(null); } return(CloneAndSetVisuals(item, prefab.gameObject, type)); }
/// <summary> /// Gets an array of the Materials on the given visual prefab type for the given item. /// These are actual references to the Materials, not a copy like Unity's Renderer.Materials[] /// </summary> public static Material[] GetMaterials(Item item, VisualPrefabType type) { Transform prefab = null; if (s_itemVisualLinks.ContainsKey(item.ItemID)) { var link = s_itemVisualLinks[item.ItemID]; switch (type) { case VisualPrefabType.VisualPrefab: prefab = link.ItemVisuals; break; case VisualPrefabType.SpecialVisualPrefabDefault: prefab = link.ItemSpecialVisuals; break; case VisualPrefabType.SpecialVisualPrefabFemale: prefab = link.ItemSpecialFemaleVisuals; break; } } if (!prefab) { prefab = GetOrigItemVisuals(item, type); } if (prefab) { var mats = new List <Material>(); foreach (var skinnedMesh in prefab.GetComponentsInChildren <SkinnedMeshRenderer>()) { mats.AddRange(skinnedMesh.materials); } foreach (var mesh in prefab.GetComponentsInChildren <MeshRenderer>()) { mats.AddRange(mesh.materials); } return(mats.ToArray()); } //SL.Log("No material found for this prefab/item!"); return(null); }
/// <summary> /// Clones the provided 'prefab' GameObject, and sets it to the provided Item and VisualPrefabType. /// </summary> /// <param name="item">The Item to apply to.</param> /// <param name="newPrefab">The visual prefab to clone and set.</param> /// <param name="type">The Type of VisualPrefab you are setting.</param> /// <returns>The cloned gameobject.</returns> public static GameObject CloneAndSetVisuals(Item item, GameObject newPrefab, VisualPrefabType type) { // Clone the visual prefab var newVisuals = UnityEngine.Object.Instantiate(newPrefab.gameObject); newVisuals.SetActive(false); UnityEngine.Object.DontDestroyOnLoad(newVisuals); // add to our CustomVisualPrefab dictionary SetVisualPrefabLink(item, newVisuals, type); // Clone the materials too so that changes to them don't affect the original item visuals foreach (var skinnedMesh in newVisuals.GetComponentsInChildren <SkinnedMeshRenderer>()) { var mats = skinnedMesh.materials; for (int i = 0; i < mats.Length; i++) { var newmat = UnityEngine.Object.Instantiate(mats[i]); UnityEngine.Object.DontDestroyOnLoad(newmat); mats[i] = newmat; } skinnedMesh.materials = mats; } foreach (var mesh in newVisuals.GetComponentsInChildren <MeshRenderer>()) { var mats = mesh.materials; for (int i = 0; i < mats.Length; i++) { var newmat = UnityEngine.Object.Instantiate(mats[i]); UnityEngine.Object.DontDestroyOnLoad(newmat); mats[i] = newmat; } mesh.materials = mats; } newVisuals.transform.parent = SL.CloneHolder; return(newVisuals); }
public static void SetVisualPrefabLink(Item item, GameObject newVisuals, VisualPrefabType type) { var link = GetOrAddVisualLink(item); switch (type) { case VisualPrefabType.VisualPrefab: link.ItemVisuals = newVisuals.transform; break; case VisualPrefabType.SpecialVisualPrefabDefault: link.ItemSpecialVisuals = newVisuals.transform; break; case VisualPrefabType.SpecialVisualPrefabFemale: link.ItemSpecialFemaleVisuals = newVisuals.transform; break; } }
public static SL_ItemVisual ParseVisualToTemplate(Item item, VisualPrefabType type, ItemVisual itemVisual) { var template = (SL_ItemVisual)Activator.CreateInstance(Serializer.GetBestSLType(itemVisual.GetType())); template.SerializeItemVisuals(itemVisual); switch (type) { case VisualPrefabType.VisualPrefab: template.ResourcesPrefabPath = item.VisualPrefabPath; break; case VisualPrefabType.SpecialVisualPrefabDefault: template.ResourcesPrefabPath = item.SpecialVisualPrefabDefaultPath; break; case VisualPrefabType.SpecialVisualPrefabFemale: template.ResourcesPrefabPath = item.SpecialVisualPrefabFemalePath; break; } ; return(template); }
/// <summary>Returns the original Item Visuals for the given Item and VisualPrefabType</summary> public static Transform GetOrigItemVisuals(Item item, VisualPrefabType type) { Transform prefab = null; switch (type) { case VisualPrefabType.VisualPrefab: prefab = ResourcesPrefabManager.Instance.GetItemVisualPrefab(item.VisualPrefabPath); break; case VisualPrefabType.SpecialVisualPrefabDefault: prefab = ResourcesPrefabManager.Instance.GetItemVisualPrefab(item.SpecialVisualPrefabDefaultPath); break; case VisualPrefabType.SpecialVisualPrefabFemale: prefab = ResourcesPrefabManager.Instance.GetItemVisualPrefab(item.SpecialVisualPrefabFemalePath); break; } return(prefab); }
internal GameObject SetCustomVisualPrefab(Item item, VisualPrefabType type, Transform newVisuals, Transform oldVisuals) { SL.Log($"Setting the {type} for {item.Name}"); var basePrefab = GameObject.Instantiate(oldVisuals.gameObject); GameObject.DontDestroyOnLoad(basePrefab); basePrefab.SetActive(false); var visualModel = UnityEngine.Object.Instantiate(newVisuals.gameObject); UnityEngine.Object.DontDestroyOnLoad(visualModel.gameObject); if (type == VisualPrefabType.VisualPrefab) { // At the moment, the only thing we replace on ItemVisuals is the 3d model, everything else is a clone. foreach (Transform child in basePrefab.transform) { // the real 3d model will always have boxcollider and meshrenderer. this is the object we want to replace. if (child.GetComponent <BoxCollider>() && child.GetComponent <MeshRenderer>()) { child.gameObject.SetActive(false); visualModel.transform.position = child.position; visualModel.transform.rotation = child.rotation; visualModel.transform.parent = child.parent; break; } } basePrefab.name = visualModel.name; CustomItemVisuals.SetVisualPrefabLink(item, basePrefab, type); SL.Log("Setup visual prefab link: " + item.Name + " -> " + basePrefab.name); return(basePrefab); } else { if (!visualModel.GetComponent <ItemVisual>() && basePrefab.GetComponent <ItemVisual>() is ItemVisual itemVisual) { if (itemVisual is ArmorVisuals) { var armorV = visualModel.AddComponent <ArmorVisuals>(); armorV.ArmorExtras = new ArmorVisualExtra[0]; } else { visualModel.AddComponent <ItemVisual>(); } } visualModel.transform.position = basePrefab.transform.position; visualModel.transform.rotation = basePrefab.transform.rotation; visualModel.gameObject.SetActive(false); // we no longer need the clone for these visuals. we should clean it up. UnityEngine.Object.DestroyImmediate(basePrefab.gameObject); CustomItemVisuals.SetVisualPrefabLink(item, visualModel, type); SL.Log("Setup visual prefab link: " + item.Name + " -> " + visualModel.name); return(visualModel); } }