static void OnGUI_SelectShader(Material mat) { string shaderName = mat.shader.name; int idx = shaderName.LastIndexOf('/'); if (idx >= 0) { shaderName = shaderName.Substring(idx + 1); } string nameList = ShaderNative.ShaderGetNameList(); string[] arr = nameList.Split(','); List <ShaderNameNode> nameNode = new List <ShaderNameNode>(); if (ImGui.BeginCombo($"Shader##Combo#ShaderName#ImporterMaterial#{s_mat_count++}", shaderName)) { for (int i = 0; i < arr.Length; ++i) { string ele = arr[i]; ShaderNameNode root = new ShaderNameNode(); DeepSplitName(ref root, ele); nameNode.Add(root); } foreach (var node in nameNode) { IterNode(node, mat.shader.name, ref mat); } ImGui.EndCombo(); } }
static void IterNode(ShaderNameNode node, string oriName, ref Material curMat) { if (node.name == null || node.name == "") { if (node.child != null) { IterNode(node.child, oriName, ref curMat); } } else { if (node.child == null) { if (ImGui.MenuItem(node.name, "", node.fullname == oriName)) { OnSelectShader(node, oriName, ref curMat); } } else { if (ImGui.BeginMenu(node.name)) { IterNode(node.child, oriName, ref curMat); ImGui.EndMenu(); } } } }
static void OnSelectShader(ShaderNameNode node, string oriName, ref Material curMat) { if (node.fullname != oriName) { curMat.shader = Shader.Find(node.fullname); Debug.LogFormat("切换了shader {0}", node.fullname); } }
static void DeepSplitName(ref ShaderNameNode parent, string name) { int idx = name.IndexOf('/'); if (idx < 0) { ShaderNameNode node = new ShaderNameNode(); node.name = name; node.parent = parent; parent.child = node; } else { string h = name.Substring(0, idx); string t = name.Substring(idx + 1); ShaderNameNode node = new ShaderNameNode(); node.name = h; node.parent = parent; parent.child = node; DeepSplitName(ref node, t); } }