internal static void GenerateCode(ComponentPathCodeGenerator getComponentCodeGenerator, out string msg, List<VariableAlias> errorComponentsList) { msg = "OK"; if (getComponentCodeGenerator == null) return; var fieldInfo = getComponentCodeGenerator.GetType().GetField("targetComponent", BindingFlags.NonPublic | BindingFlags.Instance); if (fieldInfo == null) return; var targetComponent = (Component)fieldInfo.GetValue(getComponentCodeGenerator); if (targetComponent == null) return; string targetFloder = Path.Combine(Application.dataPath, "TyGetComponentCode"); if (!Directory.Exists(targetFloder)) Directory.CreateDirectory(targetFloder); string targetComponentTypeName = targetComponent.GetType().FullName; string[] splitNames = targetComponentTypeName.Split('.'); string className = splitNames.Last(); string classNamespace = ""; if (splitNames.Length > 1) classNamespace = string.Join(".", targetComponentTypeName.Split('.').Take(splitNames.Length - 1).ToArray()); string fileName = Path.Combine(targetFloder, className + "_GetComponentCode.cs"); try { List<VariableAlias> componentList = new List<VariableAlias>(); GetVariableAlias(getComponentCodeGenerator.transform, componentList); if (File.Exists(fileName)) File.Delete(fileName); using (var fileStream = File.OpenWrite(fileName)) { using (var streamWriter = new StreamWriter(fileStream)) { streamWriter.WriteLine("//Generate by Tyrant.ComponentsForUnity.ComponentPathCodeGenerator, DO NOT MODIFY"); if (!CheckIfSameName(componentList, out msg, errorComponentsList)) { streamWriter.WriteLine("//Error:" + msg); return; } streamWriter.WriteLine("using Tyrant.ComponentsForUnity;"); if (!string.IsNullOrEmpty(classNamespace)) { streamWriter.WriteLine("namespace " + classNamespace); streamWriter.WriteLine("{"); } streamWriter.WriteLine("public partial class " + className); streamWriter.WriteLine("{"); List<VariablePathType> infos = new List<VariablePathType>(); foreach (var variableAlias in componentList) { foreach (var component in variableAlias.ComponentArray) { if (component == null) continue; string path = ""; GameObject current = component.gameObject; bool isContinue = false; while (current != getComponentCodeGenerator.gameObject) { path = current.name + "/" + path; if (current.transform.parent == null) { isContinue = true; break; } current = current.transform.parent.gameObject; } if (isContinue) continue; string typeFullName = component.GetType().FullName; string vName = Utility.GetValidVariableName(variableAlias.Alias.Trim()); streamWriter.WriteLine(string.Format(" private {0} {1};", typeFullName, vName)); infos.Add(new VariablePathType() { TypeFullName = typeFullName, VariableName = vName, Path = path.Trim('/') }); } } streamWriter.WriteLine(); streamWriter.WriteLine(" private void TyrantGetComponents()"); streamWriter.WriteLine(" {"); foreach (var info in infos) { streamWriter.WriteLine(string.Format(" {0} = this.GetComponentInChild<{1}>(\"{2}\");", info.VariableName, info.TypeFullName, info.Path)); } streamWriter.WriteLine(" }"); streamWriter.WriteLine(); streamWriter.WriteLine(" private void OnDestroyComponents()"); streamWriter.WriteLine(" {"); foreach (var info in infos) { streamWriter.WriteLine(string.Format(" {0} = null;", info.VariableName)); } streamWriter.WriteLine(" }"); if (!string.IsNullOrEmpty(classNamespace)) streamWriter.WriteLine("}"); streamWriter.Write("}"); } } } catch (Exception e) { Debug.LogException(e); if (File.Exists(fileName)) File.Delete(fileName); } }
internal static void GenerateCode(ComponentPathCodeGenerator getComponentCodeGenerator) { string msg; GenerateCode(getComponentCodeGenerator, out msg, null); }