/// <summary>Helper method to make a user friendly object name for the logs.</summary> /// <remarks> /// This method is much more intelligent than a regular <c>ToString()</c>, it can detect some /// common types and give more context on them while keeping the output short. The currently /// supported object types are: /// <list type="bullet"> /// <item>The primitive types and strings are returned as is.</item> /// <item><see cref="Part"/>. The string will have the part ID.</item> /// <item><see cref="PartModule"/>. The string will have a part ID and the module index.</item> /// <item> /// <see cref="Component"/>. The string will have the full path in the game objects hirerachy. /// </item> /// </list> /// <para> /// The other types are stringified via a regular <c>ToString()</c> call, but the value is /// prefixed with the type name. /// </para> /// </remarks> /// <param name="obj">The object to stringify. It can be <c>null</c>.</param> /// <returns>A human friendly string or the original object.</returns> /// <include file="KSPAPI_HelpIndex.xml" path="//item[@name='T:Part']"/> /// <include file="KSPAPI_HelpIndex.xml" path="//item[@name='T:PartModule']"/> /// <include file="Unity3D_HelpIndex.xml" path="//item[@name='T:UnityEngine.Transform']"/> /// <include file="Unity3D_HelpIndex.xml" path="//item[@name='T:UnityEngine.GameObject']"/> public static object ObjectToString(object obj) { if (obj == null) { return("[NULL]"); } if (obj is string || obj.GetType().IsPrimitive) { return(obj); // Skip types don't override ToString() and don't have special representaion. } var partHost = obj as Part; if (partHost != null) { return("[Part:" + DbgFormatter.PartId(partHost) + "]"); } var moduleHost = obj as PartModule; if (moduleHost != null) { var moduleNum = moduleHost.part.Modules.IndexOf(moduleHost); return("[Part:" + DbgFormatter.PartId(moduleHost.part) + "#Module:" + moduleNum + "]"); } var componentHost = obj as Component; if (componentHost != null) { return("[" + componentHost.GetType().Name + ":" + DbgFormatter.TranformPath(componentHost.transform) + "]"); } return(obj.ToString()); }
/// <summary>Helper method to make a user friendly object name for the logs.</summary> /// <remarks> /// This method is much more intelligent than a regular <c>ToString()</c>, it can detect some /// common types and give more context on them while keeping the output short. The currently /// supported object types are: /// <list type="bullet"> /// <item>The primitive types and strings are returned as is.</item> /// <item><see cref="Part"/>. The string will have the part ID.</item> /// <item><see cref="PartModule"/>. The string will have a part ID and the module index.</item> /// <item> /// <see cref="Component"/>. The string will have the full path in the game objects hirerachy. /// </item> /// </list> /// <para> /// The other types are stringified via a regular <c>ToString()</c> call, but the value is /// prefixed with the type name. /// </para> /// </remarks> /// <param name="obj">The object to stringify. It can be <c>null</c>.</param> /// <returns>A human friendly string or the original object.</returns> /// <include file="KSPAPI_HelpIndex.xml" path="//item[@name='T:Part']"/> /// <include file="KSPAPI_HelpIndex.xml" path="//item[@name='T:PartModule']"/> /// <include file="Unity3D_HelpIndex.xml" path="//item[@name='T:UnityEngine.Transform']"/> /// <include file="Unity3D_HelpIndex.xml" path="//item[@name='T:UnityEngine.GameObject']"/> public static object ObjectToString(object obj) { if (obj == null) { return("[NULL]"); } if (obj is string || obj.GetType().IsPrimitive) { return(obj); // Skip types don't override ToString() and don't have special representaion. } var partHost = obj as Part; if (partHost != null) { return("[Part:" + DbgFormatter.PartId(partHost) + "]"); } var moduleHost = obj as PartModule; if (moduleHost != null) { var moduleNum = moduleHost.part.Modules.IndexOf(moduleHost); return("[Part:" + DbgFormatter.PartId(moduleHost.part) + "#Module:" + moduleNum + "]"); } var componentHost = obj as Component; if (componentHost != null) { return("[" + componentHost.GetType().Name + ":" + DbgFormatter.TranformPath(componentHost.transform) + "]"); } if (obj is Vector3) { var vec = (Vector3)obj; return(string.Format("[Vector3:{0:0.0###},{1:0.0###},{2:0.0###}]", vec.x, vec.y, vec.z)); } if (obj is Quaternion) { var rot = (Quaternion)obj; return(string.Format("[Quaternion:{0:0.0###}, {1:0.0###}, {2:0.0###}, {3:0.0###}]", rot.x, rot.y, rot.z, rot.w)); } return(obj.ToString()); }