using System; using System.Collections.Generic; using UnityEngine; public class GameObjectKeyName { public static readonly string Level = "level"; public static readonly string LevelLiving = "level_living"; public static readonly string LevelDead = "level_dead"; // public static readonly string FadeOut = "FadeOut"; // public static readonly string LOD = "LOD_"; public static readonly string LOD_HEIGHT = "LOD_H"; public static readonly string LOD_LOW = "LOD_L"; // public static readonly string BODY_LOW = "BodyLow"; } public static class UnityComponentList where T : Component { public static int Count { get { return _list.Count; } } public static List List { get { return _list; } } public static void Begin(GameObject obj) { Begin(obj, false); } public static void Begin(GameObject obj, bool includeInactive) { _list.Clear(); if (obj != null) { obj.GetComponentsInChildren(includeInactive, _list); } } public static void End() { _list.Clear(); } private static List _list = new List(); } public static class UnityAssisstant { public static readonly string GROUP_PRO = "pro"; public static readonly string GROUP_VIB = "vib"; public static readonly string GROUP_RES = "pro"; public static readonly string GROUP_BUNDLE = "bundle"; public static readonly string GROUP_PATCH = "patch"; public static Quaternion CameraRotation = Quaternion.identity; public static float CameraYaw = 0.0f; public static void SetActive(GameObject obj, bool value) { if (obj != null) { obj.SetActive(value); } } public static void Dispose(ref T obj) where T : IDisposable { if (obj == null) { return; } obj.Dispose(); obj = default; // 这里和null本质上没有区别 } public static void DelayDispose(float delay, ref T obj) where T : IDisposable { if (obj == null) { return; } var tempObj = obj; obj = default; // 这里计时器不需要持有,因为tempObj就算被其他地方销毁了,也不会报错 ViTimerInstance.SetTime(new ViTimeNode1(), delay, node => Dispose(ref tempObj)); } // public static void Dispose(ref List list) where T : IDisposable { if (list == null) { return; } // foreach (var obj in list) { obj.Dispose(); } list.Clear(); } // public static void Dispose(ref Dictionary dictionary) where T2 : IDisposable { if (dictionary == null) { return; } // foreach (var obj in dictionary.Values) { obj.Dispose(); } dictionary.Clear(); } // public static void Dispose(ref Dictionary> dictionary) where T2 : IDisposable { if (dictionary == null) { return; } // foreach (var obj in dictionary.Values) { var list = obj; Dispose(ref list); } dictionary.Clear(); } public static void Destroy(ref T obj) where T : UnityEngine.Object { if (obj == null) { return; } UnityEngine.GameObject gameObj = obj as UnityEngine.GameObject; if (gameObj != null) { gameObj.SetActive(false); gameObj.transform.parent = null; } UnityEngine.Object.Destroy(obj); obj = null; } public static void Destroy(ref UnityEngine.GameObject obj) { if (obj == null) { return; } obj.transform.SetParent(null); obj.SetActive(false); UnityEngine.Object.Destroy(obj); obj = null; } public static void Destroy(List objList) { if (objList == null) { return; } for (int iter = 0; iter < objList.Count; ++iter) { UnityEngine.Object iterObj = objList[iter]; Destroy(ref iterObj); } objList.Clear(); } public static void Destroy(List objList) { if (objList == null) { return; } for (int iter = 0; iter < objList.Count; ++iter) { UnityEngine.GameObject iterObj = objList[iter]; Destroy(ref iterObj); } objList.Clear(); } /* public static GameObject InstantiateScene(GameObject gameObj) { GameObject newObj = UnityEngine.Object.Instantiate(gameObj); newObj.transform.localPosition = Vector3.zero; newObj.transform.localRotation = Quaternion.identity; newObj.transform.localScale = Vector3.one; newObj.SetActive(true); if (Application.isEditor) { SceneConfig sceneConfig = newObj.GetComponent(); if (sceneConfig.Skybox != null) { Int32 renderQueue = sceneConfig.Skybox.renderQueue; Shader shader = Shader.Find(sceneConfig.Skybox.shader.name); if (shader != null) { sceneConfig.Skybox.shader = shader; //sceneConfig.Skybox.renderQueue = renderQueue; } } // ReplaceMaterialShader(newObj); } return newObj; } */ public static GameObject Instantiate(GameObject gameObj, Vector3 position, Quaternion rotation) { if (gameObj == null) { return null; } GameObject newObj = UnityEngine.Object.Instantiate(gameObj, position, rotation) as GameObject; newObj.SetActive(true); if (Application.isEditor) { ReplaceMaterialShader(newObj); } return newObj; } public static GameObject Instantiate(GameObject gameObj) { if (gameObj == null) { return null; } GameObject newObj = UnityEngine.Object.Instantiate(gameObj); newObj.SetActive(true); if (Application.isEditor) { ReplaceMaterialShader(newObj); } return newObj; } public static GameObject InstantiateAsChild(GameObject gameObj, Transform tran, bool worldPosStays) { if (gameObj == null) { return null; } return InstantiateAsChild(gameObj, tran, Vector3.zero, true, worldPosStays); } public static GameObject InstantiateAsChild(GameObject gameObj, Transform tran) { if (gameObj == null) { return null; } return InstantiateAsChild(gameObj, tran, Vector3.zero, true); } public static GameObject InstantiateAsChild(GameObject gameObj, Transform tran, Vector3 offset, bool inheritScale, bool worldPosStays = true) { if (gameObj == null) { return null; } Vector3 position = Vector3.zero; Quaternion rotation = Quaternion.identity; if (tran != null) { position = tran.position + offset; rotation = tran.rotation; } GameObject newObj = UnityEngine.Object.Instantiate(gameObj, position, rotation) as GameObject; if (!inheritScale) { newObj.transform.localScale = Vector3.one; } newObj.SetActive(true); if (tran != null) { newObj.transform.SetParent(tran, worldPosStays); } if (inheritScale) { newObj.transform.localScale = Vector3.one; } if (Application.isEditor) { ReplaceMaterialShader(newObj); } return newObj; } public static void SetLossyScale(Transform tran, float value) { Transform parent = tran.parent; Vector3 scale = Vector3.one * value; if (parent != null) { Vector3 parentScale = parent.lossyScale; scale.x /= parentScale.x; scale.y /= parentScale.y; scale.z /= parentScale.z; } tran.localScale = scale; } public static void SetStaticRecursively(UnityEngine.GameObject obj, bool value) { _SetStaticRecursively(obj, value); } private static void _SetStaticRecursively(UnityEngine.GameObject obj, bool value) { obj.isStatic = value; for (int iter = 0; iter < obj.transform.childCount; ++iter) { Transform iterTran = obj.transform.GetChild(iter); _SetStaticRecursively(iterTran.gameObject, value); } } public static void SetLayerRecursively(UnityEngine.GameObject obj, int value) { _SeLayerRecursively(obj, value); } private static void _SeLayerRecursively(UnityEngine.GameObject obj, int value) { obj.layer = value; for (int iter = 0; iter < obj.transform.childCount; ++iter) { Transform iterTran = obj.transform.GetChild(iter); _SeLayerRecursively(iterTran.gameObject, value); } } public static Transform GetChildRecursively(this Transform root, string name) { return GetChildRecursively(root, name, null); } public static Transform GetChildRecursively(this Transform root, string name, Transform defaultTranform) { Transform tran = _GetChildRecursively(root, name, false); if (tran == null) { tran = _GetChildRecursively(root, name, true); } if (tran == null) { tran = defaultTranform; } return tran; } private static Transform _GetChildRecursively(Transform root, string name, bool ignoreDeactive) { if (!ignoreDeactive && !root.gameObject.activeSelf) { return null; } if (root.gameObject.name == name) { return root; } for (int iter = 0; iter < root.childCount; ++iter) { Transform iterTran = root.GetChild(iter); Transform iterTarget = _GetChildRecursively(iterTran, name, ignoreDeactive); if (iterTarget != null) { return iterTarget; } } return null; } public static string GetTranFullHierarchy(GameObject go) { if (go == null) { return string.Empty; } Transform current = go.transform; string path = current.name; while (current.parent != null) { current = current.parent; path = current.name + "/" + path; } return path; } public static void GetChildren(this GameObject parent, string name, List objList) { for (int iter = 0; iter < parent.transform.childCount; ++iter) { GameObject iterObject = parent.transform.GetChild(iter).gameObject; if (iterObject.name == name) { objList.Add(iterObject); } } } public static GameObject GetChild(this GameObject parent, string name) { Transform childTransform = parent.transform.Find(name); if (childTransform != null) { return childTransform.gameObject; } else { return null; } } public static void SetChildStateRecursively(Transform root, string name, bool value) { Transform child = GetChildRecursively(root, name, null); if (child != null) { child.gameObject.SetActive(value); } } public static void UpdateSoundVolume(GameObject obj, float volume) { if (obj == null) { return; } // UnityComponentList.Begin(obj); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { AudioSource iterComponent = UnityComponentList.List[iter]; iterComponent.volume = volume; } UnityComponentList.End(); } public static void SetSoundLoop(GameObject obj, bool value) { if (obj == null) { return; } // UnityComponentList.Begin(obj); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { AudioSource iterComponent = UnityComponentList.List[iter]; iterComponent.loop = value; } UnityComponentList.End(); } public static T GetChildComponent(GameObject parent, string name) where T : Component { Transform childTransform = parent.transform.Find(name); if (childTransform != null) { return childTransform.gameObject.GetComponent(); } else { return null; } } public static T GetComponentRecursively(GameObject obj) where T : Component { if (obj == null) { return null; } T component = obj.GetComponent(); if (component != null) { return component; } return obj.GetComponentInChildren(); } public static void GetComponentRecursively(GameObject obj, List list) where T : Component { list.Clear(); if (obj == null) { return; } obj.GetComponentsInChildren(list); //T component = obj.GetComponent(); //if (component != null) //{ // list.Add(component); //} } public static Animation GetAvatarAnimation(GameObject obj) { if (obj == null) { return null; } Animation animation = obj.GetComponentInChildren(); if (animation != null) { return animation; } //UnityComponentList.Begin() //Animation[] animationList = obj.GetComponentsInChildren(); //for (int iter = 0; iter < animationList.Length; ++iter) //{ // Animation iterAnim = animationList[iter]; // if (iterAnim["Idle"] != null) // { // return iterAnim; // } //} //if (animationList.Length > 0) //{ // return animationList[0]; //} return null; } public static void JionTransform(Transform parent, Transform child) { Quaternion oldQuat = child.transform.localRotation; if (parent != null) { child.parent = parent; } child.localPosition = Vector3.zero; child.localRotation = oldQuat; } public static void JionTransform(GameObject parent, GameObject child) { Quaternion oldQuat = child.transform.localRotation; if (parent != null) { child.transform.parent = parent.transform; } child.transform.localPosition = Vector3.zero; child.transform.localRotation = oldQuat; } public static void JionTransform(Transform parent, Transform child, Vector3 offset, Quaternion rot) { if (parent != null) { child.parent = parent; } child.localPosition = offset; child.localRotation = rot; } public static void JionTransform(GameObject parent, GameObject child, Vector3 offset, Quaternion rot) { if (parent != null) { child.transform.parent = parent.transform; } child.transform.localPosition = offset; child.transform.localRotation = rot; } public static void SetScale(GameObject obj, float scale) { SetScale(obj, new Vector3(scale, scale, scale)); } public static void SetScale(GameObject obj, Vector3 scale) { if (obj != null) { obj.transform.localScale = scale; Transform parent = obj.transform.parent; if (parent != null) { Vector3 parentScale = parent.lossyScale; scale.x /= parentScale.x; scale.y /= parentScale.y; scale.z /= parentScale.z; obj.transform.localScale = scale; } else { obj.transform.localScale = scale; } } } public static GameObject CreateChild(Transform root, string name, bool force) { Transform child = root.Find(name); if (child != null && !force) { return child.gameObject; } GameObject newObj = new GameObject(name); JionTransform(root, newObj.transform); return newObj; } public static T CreateComponent(GameObject obj) where T : Component { T component = obj.GetComponent(); if (component == null) { component = obj.AddComponent(); } return component; } public static void DelComponent(GameObject obj) where T : Component { T component = obj.GetComponent(); if (component != null) { UnityEngine.Object.Destroy(component); } } public static void SetComponentState(GameObject obj, bool active) where T : Behaviour { T component = obj.GetComponent(); if (component != null) { component.enabled = active; } } public static void SetComponentsState(GameObject obj, bool active) where T : Behaviour { UnityComponentList.Begin(obj, true); for (int iter = 0; iter < UnityComponentList.Count; ++iter) { T iterComponent = UnityComponentList.List[iter]; iterComponent.enabled = active; } UnityComponentList.End(); } #region UI public static Vector3 WorldToNGUIPixel(Camera camera, Vector3 pos) { Vector3 screenPos = camera.WorldToScreenPoint(pos); float x = screenPos.x - Screen.width * 0.5f; float y = screenPos.y - Screen.height * 0.5f; return new Vector3(x, y, 0.0f); } public static Vector3 WorldToNGUIScale(Camera camera, Vector3 pos) { Vector3 screenPos = camera.WorldToScreenPoint(pos); float horizScale = (float)Screen.width / (float)Screen.height; float x = (1.0f + (screenPos.x - Screen.width) / Screen.width * 2) * horizScale; float y = 1.0f + (screenPos.y - Screen.height) / Screen.height * 2; return new Vector3(x, y, 0.0f); } public static Vector3 ScreenToNGUIScale(Vector3 screenPos) { float horizScale = (float)Screen.width / (float)Screen.height; float x = (1.0f + (screenPos.x - Screen.width) / Screen.width * 2) * horizScale; float y = 1.0f + (screenPos.y - Screen.height) / Screen.height * 2; return new Vector3(x, y, 0.0f); } #endregion UI #region Gree&Angle public static float Translate360To2Pi(float value) { return (180.0f - value) * 3.14f / 180.0f; } public static float Translate2PiTo360(float value) { return 180.0f - value / 3.14f * 180.0f; } #endregion Gree&Angle //public static void SetColor(Renderer renderer, Color color) //{ // Material[] materials = renderer.materials; // for (int iter = 0; iter < materials.Length; ++iter) // { // Material material = materials[iter]; // material.color = color; // } //} #region Color public static void SetColor(Renderer renderer, Color color, Dictionary oldColors) { Material[] materials = renderer.materials; for (int iter = 0; iter < materials.Length; ++iter) { Material material = materials[iter]; if (!material.HasProperty("_Color")) { continue; } if (oldColors != null) { oldColors[material.GetInstanceID()] = material.color; } material.color = color; } } public static void SetColor(List renderers, Color color, Dictionary oldColors) { for (int iter = 0; iter < renderers.Count; ++iter) { SetColor(renderers[iter], color, oldColors); } } public static void SetColor(Renderer renderer, Dictionary colors) { Material[] materials = renderer.materials; for (int iter = 0; iter < materials.Length; ++iter) { Material material = materials[iter]; if (!material.HasProperty("_Color")) { continue; } Color color; if (colors.TryGetValue(material.GetInstanceID(), out color)) { material.color = color; } } } public static void SetColor(List renderers, Dictionary colors) { for (int iter = 0; iter < renderers.Count; ++iter) { SetColor(renderers[iter], colors); } } public static void SetColor(GameObject obj, Color color) { UnityComponentList.Begin(obj); SetColor(UnityComponentList.List, null); UnityComponentList.End(); } public static void SetColor(GameObject obj, Color color, Dictionary oldColors) { UnityComponentList.Begin(obj); SetColor(UnityComponentList.List, color, oldColors); UnityComponentList.End(); } public static void SetColor(GameObject obj, Dictionary colors) { UnityComponentList.Begin(obj); SetColor(UnityComponentList.List, colors); UnityComponentList.End(); } #endregion Color public static void Rewind(GameObject obj) { UnityComponentList.Begin(obj); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { Animation iterComponent = UnityComponentList.List[iter]; iterComponent.Rewind(); } UnityComponentList.End(); // UnityComponentList.Begin(obj); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { Animator iterComponent = UnityComponentList.List[iter]; iterComponent.StopPlayback(); } UnityComponentList.End(); // UnityComponentList.Begin(obj); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { ParticleSystem iterComponent = UnityComponentList.List[iter]; iterComponent.time = 0.0f; iterComponent.Play(); } UnityComponentList.End(); // } // public static void ReplaceMaterialShader(Material material) { if (material == null) { return; } // Int32 renderQueue = material.renderQueue; Shader shader = Shader.Find(material.shader.name); if (shader != null) { #if UNITY_EDITOR if (!material.isVariant) { material.shader = shader; } #else material.shader = shader; #endif material.renderQueue = renderQueue; } } public static void ReplaceMaterialShader(Renderer renderer) { for (int iter = 0; iter < renderer.sharedMaterials.Length; ++iter) { Material iterMaterial = renderer.sharedMaterials[iter]; if (iterMaterial != null) { ReplaceMaterialShader(iterMaterial); } } } public static void ReplaceMaterialShader(GameObject gameobject) { UnityComponentList.Begin(gameobject, true); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { ReplaceMaterialShader(UnityComponentList.List[iter]); } UnityComponentList.End(); } public static SkinnedMeshRenderer GetMainSkin(GameObject obj) { SkinnedMeshRenderer skin = obj.GetComponent(); if (skin != null) { return skin; } // float maxBody = 0.0f; UnityComponentList.Begin(obj); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { SkinnedMeshRenderer iterComponent = UnityComponentList.List[iter]; Vector3 iterSize = iterComponent.bounds.size; float iterBody = iterSize.x * iterSize.y * iterSize.z; if (iterBody >= maxBody) { maxBody = iterBody; skin = iterComponent; } } UnityComponentList.End(); return skin; } public static void SetParticleLightEnable(GameObject obj, bool paritcalLight) { if (!paritcalLight) { return; } // UnityComponentList.Begin(obj); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { Light iterComponent = UnityComponentList.List[iter]; iterComponent.enabled = true; } UnityComponentList.End(); } public delegate Material GhostMaterialReplace(Material old, Material newTemplate); public static GameObject CreateGhostAvatar(GameObject root, SkinnedMeshRenderer renderer, Material material, GhostMaterialReplace materialReplace, List ownMeshList) { if (renderer.sharedMesh == null) { return null; } GameObject newObj = UnityAssisstant.CreateChild(root.transform, renderer.gameObject.name, false); SkinnedMeshRenderer mesh = UnityAssisstant.CreateComponent(newObj); mesh.sharedMesh = (UnityEngine.Mesh)UnityEngine.Object.Instantiate(renderer.sharedMesh); mesh.bones = renderer.bones; Material newMaterial = material; int materialSize = renderer.materials.Length; UnityEngine.Material[] materials = new UnityEngine.Material[materialSize]; for (int iter = 0; iter < materialSize; ++iter) { if (materialReplace != null) { newMaterial = materialReplace(renderer.materials[iter], material); } // materials[iter] = newMaterial; } mesh.material = newMaterial; mesh.materials = materials; mesh.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; mesh.receiveShadows = false; if (ownMeshList != null) { ownMeshList.Add(mesh.sharedMesh); } return newObj; } public static GameObject CreateGhostAvatar(GameObject root, MeshRenderer renderer, Material material, GhostMaterialReplace materialReplace, List ownMeshList) { GameObject newObj = UnityAssisstant.CreateChild(root.transform, renderer.gameObject.name, false); SkinnedMeshRenderer mesh = UnityAssisstant.CreateComponent(newObj); mesh.sharedMesh = (UnityEngine.Mesh)UnityEngine.Object.Instantiate(renderer.additionalVertexStreams); Material newMaterial = material; if (materialReplace != null) { newMaterial = materialReplace(mesh.material, material); } int materialSize = renderer.materials.Length; UnityEngine.Material[] materials = new UnityEngine.Material[materialSize]; for (int iter = 0; iter < materialSize; ++iter) { materials[iter] = newMaterial; } mesh.material = newMaterial; mesh.materials = materials; mesh.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; mesh.receiveShadows = false; if (ownMeshList != null) { ownMeshList.Add(mesh.sharedMesh); } return newObj; } public static GameObject BakeMesh(GameObject gameobject, Material replaceMaterial) { if (gameobject == null) { return null; } // GameObject shadowRoot = new GameObject(); shadowRoot.name = gameobject.name + "_shadowRoot"; // UnityComponentList.Begin(gameobject); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { SkinnedMeshRenderer iterSkinrenderer = UnityComponentList.List[iter]; if (iterSkinrenderer == null) { continue; } // Mesh mesh = new Mesh(); iterSkinrenderer.BakeMesh(mesh); // GameObject iterChildrenobj = CreatMeshGameObject(mesh, iterSkinrenderer, replaceMaterial); // iterChildrenobj.transform.position = iterSkinrenderer.gameObject.transform.position - gameobject.transform.position; iterChildrenobj.transform.rotation = iterSkinrenderer.gameObject.transform.rotation; iterChildrenobj.transform.parent = shadowRoot.transform; } UnityComponentList.End(); // UnityComponentList.Begin(gameobject); for (int iter = 0; iter < UnityComponentList.List.Count; ++iter) { MeshFilter iterMeshFilter = UnityComponentList.List[iter]; if (!iterMeshFilter.gameObject.name.StartsWith("Wp")) { continue; } MeshRenderer iterMeshrenderer = iterMeshFilter.gameObject.GetComponent(); if (iterMeshFilter == null || iterMeshrenderer == null) continue; // Mesh mesh = UnityEngine.Object.Instantiate(iterMeshFilter.mesh); // GameObject childrenobj = CreatMeshGameObject(mesh, iterMeshrenderer, replaceMaterial); // childrenobj.transform.position = iterMeshrenderer.gameObject.transform.position - gameobject.transform.position; childrenobj.transform.rotation = iterMeshrenderer.gameObject.transform.rotation; childrenobj.transform.localScale = iterMeshrenderer.gameObject.transform.lossyScale; childrenobj.transform.parent = shadowRoot.transform; } UnityComponentList.End(); // shadowRoot.transform.position = gameobject.transform.position; StaticBatchingUtility.Combine(shadowRoot); return shadowRoot; } public static GameObject CreatMeshGameObject(Mesh mesh, Renderer render, Material replaceMaterial) { GameObject childrenobj = new GameObject(); MeshFilter meshfilter = childrenobj.AddComponent(); MeshRenderer meshrender = childrenobj.AddComponent(); meshfilter.mesh = mesh; // if (replaceMaterial) { Material[] materials = new Material[render.materials.Length]; for (int iter = 0; iter < materials.Length; ++iter) { materials[iter] = replaceMaterial; } meshrender.materials = materials; } else { meshrender.materials = render.materials; } // meshrender.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; meshrender.receiveShadows = false; meshrender.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off; // return childrenobj; } public static Material MaterialTextureAlphaReplace(Material oldMaterial, Material newMaterialTemplate) { if (newMaterialTemplate.HasProperty("_OriginalTex")) { Material newMaterial = new Material(newMaterialTemplate.shader); newMaterial.CopyPropertiesFromMaterial(newMaterialTemplate); Texture meshMainTexture = null; if (oldMaterial.HasProperty("_MainTex")) { meshMainTexture = oldMaterial.GetTexture("_MainTex"); } if (meshMainTexture != null) { newMaterial.SetTexture("_OriginalTex", meshMainTexture); } else { Texture2D texture = new Texture2D(1, 1, TextureFormat.ARGB32, false); texture.SetPixel(0, 0, Color.black); newMaterial.SetTexture("_OriginalTex", texture); newMaterial.SetColor("_RimColor", Color.black); } return newMaterial; } else { return newMaterialTemplate; } } public static Material MaterialToonShaderTextureReplace(Material oldMaterial, Material newMaterialTemplate) { if (newMaterialTemplate.HasProperty("_MainTex")) { Material newMaterial = new Material(newMaterialTemplate.shader); newMaterial.CopyPropertiesFromMaterial(newMaterialTemplate); Texture meshMainTexture = null; if (oldMaterial.HasProperty("_MainTex")) { meshMainTexture = oldMaterial.GetTexture("_MainTex"); newMaterial.SetTexture("_MainTex", meshMainTexture); } if (oldMaterial.HasProperty("_Ramp")) { newMaterial.SetTexture("_Ramp", oldMaterial.GetTexture("_Ramp")); } if (oldMaterial.HasProperty("_SpecColor")) { newMaterial.SetColor("_SpecColor", oldMaterial.GetColor("_SpecColor")); } if (oldMaterial.HasProperty("_Shininess")) { newMaterial.SetFloat("_Shininess", oldMaterial.GetFloat("_Shininess")); } if (oldMaterial.HasProperty("_Color")) { newMaterial.SetColor("_Color", oldMaterial.GetColor("_Color")); } if (oldMaterial.HasProperty("_SColor")) { newMaterial.SetColor("_SColor", oldMaterial.GetColor("_SColor")); } if (oldMaterial.HasProperty("_Outline")) { newMaterial.SetFloat("_Outline", oldMaterial.GetFloat("_Outline")); } if (oldMaterial.HasProperty("_OutlineColor")) { newMaterial.SetColor("_OutlineColor", oldMaterial.GetColor("_OutlineColor")); } if (oldMaterial.HasProperty("_Emission")) { newMaterial.SetColor("_Emission", oldMaterial.GetColor("_Emission")); } // return newMaterial; } else { return newMaterialTemplate; } } public static Material MaterialMatCapTextureAddReplace(Material oldMaterial, Material newMaterialTemplate) { if (newMaterialTemplate.HasProperty("_MainTex")) { Material newMaterial = new Material(newMaterialTemplate.shader); newMaterial.CopyPropertiesFromMaterial(newMaterialTemplate); Texture meshMainTexture = null; if (oldMaterial.HasProperty("_MainTex")) { meshMainTexture = oldMaterial.GetTexture("_MainTex"); newMaterial.SetTexture("_MainTex", meshMainTexture); } if (oldMaterial.HasProperty("_MatCap")) { newMaterial.SetTexture("_MatCap", oldMaterial.GetTexture("_MatCap")); } // return newMaterial; } else { return newMaterialTemplate; } } public static Material CharacterShaderReplace(Material oldMaterial, Material newMaterialTemplate) { if (newMaterialTemplate.HasProperty("_MainTex")) { Material newMaterial = new Material(newMaterialTemplate.shader); newMaterial.CopyPropertiesFromMaterial(newMaterialTemplate); Texture meshMainTexture = null; if (oldMaterial.HasProperty("_MainTex")) { meshMainTexture = oldMaterial.GetTexture("_MainTex"); newMaterial.SetTexture("_MainTex", meshMainTexture); } if (oldMaterial.HasProperty("_MatTex")) { newMaterial.SetTexture("_MatTex", oldMaterial.GetTexture("_MatTex")); } if (oldMaterial.HasProperty("_DiffuseRampTex")) { newMaterial.SetTexture("_DiffuseRampTex", oldMaterial.GetTexture("_DiffuseRampTex")); } if (oldMaterial.HasProperty("_SpecRampTex")) { newMaterial.SetTexture("_SpecRampTex", oldMaterial.GetTexture("_SpecRampTex")); } if (oldMaterial.HasProperty("_OutlineWidth")) { newMaterial.SetFloat("_OutlineWidth", oldMaterial.GetFloat("_OutlineWidth")); } if (oldMaterial.HasProperty("_EdgeColor")) { newMaterial.SetColor("_EdgeColor", oldMaterial.GetColor("_EdgeColor")); } if (oldMaterial.HasProperty("_LightParams")) { newMaterial.SetVector("_LightParams", oldMaterial.GetVector("_LightParams")); } if (oldMaterial.HasProperty("_LightParamsHDR")) { newMaterial.SetVector("_LightParamsHDR", oldMaterial.GetVector("_LightParamsHDR")); } if (oldMaterial.HasProperty("_HairParams")) { newMaterial.SetVector("_HairParams", oldMaterial.GetVector("_HairParams")); } if (oldMaterial.HasProperty("_AnimeEnvTex")) { newMaterial.SetTexture("_AnimeEnvTex", oldMaterial.GetTexture("_AnimeEnvTex")); } if (oldMaterial.HasProperty("_EnvParams")) { newMaterial.SetVector("_EnvParams", oldMaterial.GetVector("_EnvParams")); } if (oldMaterial.HasProperty("_SceneEnvTex")) { newMaterial.SetTexture("_SceneEnvTex", oldMaterial.GetTexture("_SceneEnvTex")); } // return newMaterial; } else { return newMaterialTemplate; } } public static void SelectShareMaterial(GameObject obj, Dictionary materialList) { UnityComponentList.Begin(obj); SelectShareMaterial(UnityComponentList.List, materialList); UnityComponentList.End(); } public static void SelectShareMaterial(List rendererList, Dictionary materialList) { for (int iter = 0; iter < rendererList.Count; ++iter) { SelectShareMaterial(rendererList[iter], materialList); } } public static void SelectShareMaterial(Renderer renderer, Dictionary materialList) { materialList[renderer.GetInstanceID()] = renderer.sharedMaterials; } public static void RevertShareMaterial(GameObject obj, Dictionary materialList) { UnityComponentList.Begin(obj); RevertShareMaterial(UnityComponentList.List, materialList); UnityComponentList.End(); } public static void RevertShareMaterial(List rendererList, Dictionary materialList) { for (int iter = 0; iter < rendererList.Count; ++iter) { RevertShareMaterial(rendererList[iter], materialList); } } public static void RevertShareMaterial(Renderer renderer, Dictionary materialList) { Material[] materials; if (materialList.TryGetValue(renderer.GetInstanceID(), out materials)) { renderer.materials = materials; } } private static Vector3 Pick(Vector3 pos, float radius, Vector3 direction, float distance, int layerMask, int reserveCount) { RaycastHit hitResult; if (Physics.SphereCast(pos, radius, direction, out hitResult, distance, layerMask)) { float newDistance = Math.Max(hitResult.distance - 0.01f, 0.0f); Vector3 newPos = pos + direction * newDistance; // if (reserveCount > 1) { float cutedDistance = distance - newDistance; Vector3 cutedValue = direction * cutedDistance; Vector3 normal = -hitResult.normal; Vector3 normalValue = Vector3.Dot(cutedValue, normal) * normal; Vector3 slideValue = cutedValue - normalValue; Vector3 slideDir = slideValue.normalized; float slideDistance = slideValue.magnitude; return Pick(newPos, radius, slideDir, slideDistance, layerMask, reserveCount - 1); } else { return newPos; } } else { return pos + direction * distance; } } public static Vector3 ViewportCenterPos = new Vector3(0.5f, 0.5f, 0.0f); public static Vector3 WorldToViewportPoint(Camera camera, Vector3 worldPos, float frontDistance) { Ray ray = camera.ViewportPointToRay(ViewportCenterPos); Vector3 diff = worldPos - ray.origin; diff -= Vector3.Dot(diff, ray.direction) * ray.direction; worldPos = ray.origin + diff + frontDistance * ray.direction; return camera.WorldToViewportPoint(worldPos); } public static Vector3 ViewportRoundPoint(Vector3 viewPortPos) { Vector3 viewPortDiff = viewPortPos - ViewportCenterPos; viewPortDiff /= (2.0f * ViMathDefine.Max(ViMathDefine.Abs(viewPortDiff.x), ViMathDefine.Abs(viewPortDiff.y))); return ViewportCenterPos + viewPortDiff; } public static bool WorldPointInViewport(Camera camera, Vector3 worldPos) { Vector3 viewPos = camera.WorldToViewportPoint(worldPos); if (0.0f < viewPos.x && viewPos.x < 1.0f && 0.0f < viewPos.y && viewPos.y < 1.0f) { return true; } else { return false; } } // public static string GetDeviceId() { const string key = "device_id"; if (PlayerPrefs.HasKey(key)) { return PlayerPrefs.GetString(key); } string newId = System.Guid.NewGuid().ToString(); PlayerPrefs.SetString(key, newId); PlayerPrefs.Save(); return newId; } }