上海虹口龙之梦项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

608 lines
26 KiB

8 months ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using UnityEngine.UI;
namespace AX.GeneralTools
{
public enum RenameType
{
,
,
,
,
}
public enum RenderingMode
{
Opaque = 0,
Cutout = 1,
Fade = 2,
Transparent = 3
}
public enum ObjectMode
{
,
,
}
public enum CommonMaterial
{
,
,
,
,
,
,
}
public enum ShadowType
{
On = 0,
Off = 1,
TwoSided = 2,
ShadowsOnly = 3,
}
public enum CompentType
{
,
,
}
public enum ComponentMode
{
BoxCollider,
MeshCollider,
CapsuleCollider,
CharacterControler,
LineRenderer,
TrailRenderer,
NavMeshAgent,
Animator,
AudioSource,
VideoPlayer,
}
public enum DefualtCollider
{
None,
MeshCollider,
BoxCollider,
}
public enum PowerMode
{
None,
NameAndTask,
}
public class GeneralTools:Editor
{
// 重命名
public static RenameType renameType;
public static bool isRenameTools = true;
public static string BaseName = "Object";
public static int BaseCount = 0;
public static int Increment = 1;
public static string Prefix = "Prefix_";
public static string Suffix = "_Suffix";
public static string FindName = "";
public static string ReplaceName = "";
//常用材质
public static CommonMaterial commonMaterial;
public static bool isCommonMaterial = true;
////切换标准材质为通用渲染管线材质
//public static bool isConvertShander = true;
////public static Color m_BaseColor = Color.gray;
////public static Texture2D m_BaseMap;
////public static float m_Metallic;
////public static Texture2D m_MetallicGlossMap;
////public static float m_Smothness;
////public static Texture2D m_BumpMap;
////public static Material m_Material;
//public static Material[] m_Materials = new Material[0];
//转换RenderingMode
public static ObjectMode objectMode;
public static RenderingMode defaultMode = RenderingMode.Transparent;
public static RenderingMode targetMode = RenderingMode.Fade;
public static bool isRenderingMode = true;
//转换材质颜色
public static bool isMainColor = true;
public static ObjectMode SelectMode = ObjectMode.;
public static Color defaultColor = new Color(0.588f, 0.588f, 0.588f, 1f);
public static Color targetColor = Color.white;
//设置标准材质属性
public static bool isSetMaterialAttiure = true;
public static float metallic = 0;
public static float smoothness = 0;
public static Color emissionColor = Color.black;
public static ShadowType shadowType;
//添加常用组件
public static bool isCompent = true;
public static CompentType compentType;
//public static ComponentMode componentMode;
//设置物体属性
public static bool isAttribut = true;
public static string NormalTag = "Untagged";
public static int NormalLayer = 0;
public static DefualtCollider defualtCollider;
public static bool isAddInstantiateObjectInfo;
public static int FloorNum = 0;
//替换prefab
public static bool isReplacePrefab = true;
public static GameObject OriginalPrefab;
public static bool isName=true;
public static bool isParent=true;
public static bool isPosition = true;
public static bool isRotation = true;
public static bool isScale = true;
public static bool isDestroyOriginalPrefab = false;
//设置物体父物体
public static bool isSetParent = true;
public static Transform Parent;
//反转法线
public static bool isFlip=true;
//显示选择物体路径
public static bool isShowSelectedName=true;
//Power
public static bool ispowerSet = true;
public static PowerMode powerMode;
public static GameObject Body;
public static Font font;
private static Vector3 size;
public static void FlipNormal()
{
GameObject obj = Selection.activeGameObject;
Vector3[] normals = obj.GetComponent<MeshFilter>().sharedMesh.normals;
for (int i = 0; i < normals.Length; i++)
{
normals[i] = -normals[i];
}
obj.GetComponent<MeshFilter>().sharedMesh.normals = normals;
int[] triangles = obj.GetComponent<MeshFilter>().sharedMesh.triangles;
for (int i = 0; i < triangles.Length; i += 3)
{
int t = triangles[i];
triangles[i] = triangles[i + 2];
triangles[i + 2] = t;
}
obj.GetComponent<MeshFilter>().sharedMesh.triangles = triangles;
}
public static void SetParent()
{
foreach(GameObject go in Selection.gameObjects)
{
go.transform.parent = Parent;
}
}
//重命名
public static void OnRename()
{
if (Selection.objects == null)
return;
int PostFix = BaseCount;
List<GameObject> mySelection = new List<GameObject>(Selection.gameObjects);
mySelection.Sort((go1, go2) => go1.transform.GetSiblingIndex().CompareTo(go2.transform.GetSiblingIndex()));
foreach (GameObject obj in mySelection)
{
obj.name = BaseName + PostFix;
PostFix += Increment;
BaseCount = PostFix;
}
}
//添加前缀
public static void OnAddPrefix()
{
foreach (GameObject obj in Selection.gameObjects)
{
obj.name = Prefix + obj.name;
}
}
//添加后缀
public static void OnAddSuffix()
{
foreach (GameObject obj in Selection.gameObjects)
{
obj.name = obj.name + Suffix;
}
}
//查找替换
public static void OnReplace()
{
foreach (GameObject obj in Selection.gameObjects)
{
string newName = obj.name.Replace(FindName, ReplaceName);
obj.name = newName;
}
}
//常用材质
public static void OnCommomMaterial(CommonMaterial _commonMaterial)
{
foreach (GameObject obj in Selection.gameObjects)
{
if (obj.GetComponent<MeshRenderer>() != null)
{
foreach (Material m in obj.GetComponent<MeshRenderer>().sharedMaterials)
{
switch (_commonMaterial)
{
case CommonMaterial.:
m.shader = Shader.Find("Universal Render Pipeline/Lit");
m.SetInt("_Surface", 0);
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
m.SetInt("_ZWrite", 1);
m.DisableKeyword("_ALPHATEST_ON");
m.DisableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = -1;
obj.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
m.SetFloat("_Metallic", 0);
m.SetFloat("_Smoothness", 0);
break;
case CommonMaterial.:
m.shader = Shader.Find("Universal Render Pipeline/Lit");
m.SetInt("_Surface", 0);
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
m.SetInt("_ZWrite", 1);
m.DisableKeyword("_ALPHATEST_ON");
m.DisableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = -1;
obj.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
m.SetFloat("_Metallic", 1);
m.SetFloat("_Smoothness", 1);
break;
case CommonMaterial.:
m.shader = Shader.Find("Universal Render Pipeline/Lit");
m.SetInt("_Surface", 1);
m.SetColor("_BaseColor", Color.white);
//m.SetFloat("_Mode", 2);
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m.SetInt("_ZWrite", 0);
m.DisableKeyword("_ALPHATEST_ON");
m.EnableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = 3000;
obj.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
m.SetFloat("_Metallic", 0);
m.SetFloat("_Smoothness", 0);
break;
case CommonMaterial.:
m.shader = Shader.Find("Standard");
8 months ago
m.SetInt("_Surface", 1);
m.SetColor("_BaseColor", new Color(0.588f, 0.784f, 0.843f, 0.47f));
m.SetFloat("_Mode", 3);
8 months ago
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m.SetInt("_ZWrite", 0);
m.DisableKeyword("_ALPHATEST_ON");
m.DisableKeyword("_ALPHABLEND_ON");
m.EnableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = 3000;
obj.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
m.SetFloat("_Metallic", 0);
m.SetFloat("_Smoothness", 0);
m.SetFloat("_SpecularHighlights", 0);
break;
case CommonMaterial.:
m.shader = Shader.Find("Standard");
8 months ago
m.SetInt("_Surface", 1);
m.SetColor("_BaseColor", new Color(0.55f, 0.63f, 0.71f, 0.7843f));
m.SetFloat("_Mode", 3);
8 months ago
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
m.SetInt("_ZWrite", 0);
m.DisableKeyword("_ALPHATEST_ON");
m.DisableKeyword("_ALPHABLEND_ON");
m.EnableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = 3000;
obj.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
m.SetFloat("_Metallic", 0);
m.SetFloat("_Smoothness", 0);
m.SetFloat("_SpecularHighlights", 0);
break;
case CommonMaterial.:
m.shader = Shader.Find("Universal Render Pipeline/Lit");
m.SetInt("_Surface", 0);
m.SetColor("_BaseColor", Color.white);
//m.SetFloat("_Mode", 0);
m.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
m.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
m.SetInt("_ZWrite", 1);
m.DisableKeyword("_ALPHATEST_ON");
m.DisableKeyword("_ALPHABLEND_ON");
m.DisableKeyword("_ALPHAPREMULTIPLY_ON");
m.renderQueue = -1;
obj.GetComponent<MeshRenderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
m.SetFloat("_Metallic", 0);
m.SetFloat("_Smoothness", 0.95f);
break;
}
}
}
}
}
//public static void CovertShaderAttrube()
//{
// m_Materials = Selection.activeGameObject.GetComponent<Renderer>().sharedMaterials;
// for(int i = 0; i < m_Materials.Length; i++)
// {
// if(m_Materials[i].shader== Shader.Find("Standard"))
// {
// var _BaseColor = m_Materials[i].GetColor("_Color");
// var _BaseMap = m_Materials[i].GetTexture("_MainTex");
// var _Metallic = m_Materials[i].GetFloat("_Metallic");
// var _MetallicGlossMap = m_Materials[i].GetTexture("_MetallicGlossMap");
// var _Smoothness = m_Materials[i].GetFloat("_Glossiness");
// var _BumpMap = m_Materials[i].GetTexture("_BumpMap");
// m_Materials[i].shader = Shader.Find("Universal Render Pipeline/Lit");
// m_Materials[i].SetColor("_BaseColor", _BaseColor);
// m_Materials[i].SetTexture("_BaseMap", _BaseMap);
// m_Materials[i].SetFloat("_Metallic", _Metallic);
// m_Materials[i].SetTexture("_MetallicGlossMap", _MetallicGlossMap);
// m_Materials[i].SetFloat("_Smoothness", _Smoothness);
// m_Materials[i].SetTexture("_BumpMap", _BumpMap);
// }
// }
//if (m_Material != null&&m_Material.shader==Shader.Find("Standard"))
//{
// m_BaseColor = m_Material.GetColor("_Color");
// m_BaseMap = (Texture2D)m_Material.GetTexture("_MainTex");
// m_Metallic = m_Material.GetFloat("_Metallic");
// m_MetallicGlossMap = m_Material.GetTexture("_MetallicGlossMap") as Texture2D;
// m_Smothness = m_Material.GetFloat("_Glossiness");
// m_BumpMap = m_Material.GetTexture("_BumpMap") as Texture2D;
//}
//}
//public static void SetShaderAttrube()
//{
// //if (m_Material != null && m_Material.shader == Shader.Find("Standard"))
// //{
// // m_Material.shader = Shader.Find("Universal Render Pipeline/Lit");
// // m_Material.SetColor("_BaseColor", m_BaseColor);
// // m_Material.SetTexture("_BaseMap", m_BaseMap);
// // m_Material.SetFloat("_Metallic", m_Metallic);
// // m_Material.SetTexture("_MetallicGlossMap", m_MetallicGlossMap);
// // m_Material.SetFloat("_Smoothness", m_Smothness);
// // m_Material.SetTexture("_BumpMap", m_BumpMap);
// //}
//}
//切换颜色
public static void OnMainColor()
{
switch (SelectMode)
{
case ObjectMode.:
foreach (GameObject obj in GameObject.FindObjectsOfType(typeof(GameObject)))
{
if (obj.GetComponent<MeshRenderer>() != null)
{
foreach (Material m in obj.GetComponent<MeshRenderer>().sharedMaterials)
{
if (m.shader == Shader.Find("Universal Render Pipeline/Lit"))
{
if (m.GetColor("_BaseColor") == defaultColor)
{
m.SetColor("_BaseColor", targetColor);
}
}
}
}
}
break;
case ObjectMode.:
foreach (GameObject o in Selection.gameObjects)
{
if (o.GetComponent<MeshRenderer>() != null)
{
foreach (Material m in o.GetComponent<MeshRenderer>().sharedMaterials)
{
if (m.shader == Shader.Find("Universal Render Pipeline/Lit") )
{
m.SetColor("_BaseColor", targetColor);
}
}
}
}
break;
}
}
//添加或删除组建
public static void Compents(ComponentMode componentMode)
{
bool isAdd = true;
switch (compentType)
{
case CompentType.:
isAdd = true;
break;
case CompentType.:
isAdd = false;
break;
}
if (Selection.activeGameObject)
{
foreach(GameObject obj in Selection.gameObjects)
{
switch (componentMode)
{
case ComponentMode.BoxCollider:
if (isAdd)
{
if (obj.GetComponent<MeshRenderer>() != null || obj.GetComponent<SkinnedMeshRenderer>() != null)
{
if (!obj.GetComponent<BoxCollider>())
{
obj.AddComponent<BoxCollider>();
}
else
{
Debug.LogWarning("BoxCollider" + "组件已存在");
}
}
}
else
{
if (obj.GetComponent<BoxCollider>())
{
DestroyImmediate(obj.GetComponent<BoxCollider>());
}
else
{
Debug.LogWarning("BoxCollider组件不存在!");
}
}
break;
case ComponentMode.MeshCollider:
if (isAdd)
{
if (obj.GetComponent<MeshRenderer>() != null || obj.GetComponent<SkinnedMeshRenderer>() != null)
{
if (!obj.GetComponent<MeshCollider>())
{
obj.AddComponent<MeshCollider>();
}
else
{
Debug.LogWarning("MeshCollider" + "组件已存在");
}
}
}
else
{
if (obj.GetComponent<MeshCollider>())
{
DestroyImmediate(obj.GetComponent<MeshCollider>());
}
else
{
Debug.LogWarning("MeshCollider组件不存在!");
}
}
break;
}
}
}
}
//设置物体属性
public static void SetObjectAttribute()
{
foreach (GameObject obj in Selection.gameObjects)
{
obj.tag = NormalTag;
obj.layer = NormalLayer;
if (obj.GetComponent<MeshRenderer>() ||obj.GetComponent<SkinnedMeshRenderer>())
{
switch (defualtCollider)
{
case DefualtCollider.None:
return;
case DefualtCollider.BoxCollider:
if (!obj.GetComponent<BoxCollider>())
obj.AddComponent<BoxCollider>();
else
Debug.LogWarning("BoxCollider已存在");
break;
case DefualtCollider.MeshCollider:
if (!obj.GetComponent<MeshCollider>())
obj.AddComponent<MeshCollider>();
else
Debug.LogWarning("MeshCollider");
break;
}
}
//if (isAddInstantiateObjectInfo)
//{
// obj.AddComponent<InstantiateObjectInfo>();
// obj.GetComponent<InstantiateObjectInfo>().FloorNum = FloorNum;
//}
}
}
public static void OnReplacePrefab()
{
//foreach(GameObject obj in Selection.gameObjects)
//{
// GameObject op = PrefabUtility.InstantiatePrefab(OriginalPrefab) as GameObject;
// if (isName) op.name = obj.name;
// if (isParent) op.transform.parent = obj.transform.parent;
// if (isPosition) op.transform.localPosition = obj.transform.localPosition;
// if (isRotation) op.transform.localRotation = obj.transform.localRotation;
// if (isScale) op.transform.localScale = obj.transform.localScale;
// if(isDestroyOriginalPrefab) DestroyImmediate(obj);
//}
foreach (GameObject go in Selection.gameObjects)
{
GameObject op = PrefabUtility.InstantiatePrefab(OriginalPrefab) as GameObject;
op.GetComponent<RectTransform>().pivot = go.GetComponent<RectTransform>().pivot;
op.GetComponent<RectTransform>().anchorMin = go.GetComponent<RectTransform>().anchorMin;
op.GetComponent<RectTransform>().anchorMax = go.GetComponent<RectTransform>().anchorMax;
op.GetComponent<RectTransform>().anchoredPosition = go.GetComponent<RectTransform>().anchoredPosition;
op.GetComponent<RectTransform>().sizeDelta = go.GetComponent<RectTransform>().sizeDelta;
//op.GetComponent<Text>().text = go.GetComponent<Text>().text;
//op.GetComponent<Text>().fontSize = (int)go.GetComponent<Text>().fontSize;
//if(go.GetComponent<TMP_Text>().alignment == )
//op.GetComponent<Text>().alignment = (TextAnchor)go.GetComponent<TMP_Text>().alignment;
op.name = go.name;
op.transform.SetParent(go.transform.parent, false);
}
//var go = Selection.activeGameObject;
}
public static string GetName(Transform parent, string myName)
{
if (parent != null)
{
string tempName = $"{parent.name}/{myName}";
return GetName(parent.parent, tempName);
}
else
{
int index = myName.IndexOf($"/");
myName = myName.Substring(index + 1);
index = myName.IndexOf($"/");
myName = myName.Substring(index + 1);
Debug.Log(myName);
return myName;
}
}
}
}