예제 #1
0
    protected string Write(string sName, object content)
    {
        string[] stlist = content as string[];
        if (null != stlist)
        {
            string sWrite = "";
            for (int i = 0; i < stlist.Length; ++i)
            {
                sWrite += (0 == i) ? stlist[i] : (";" + stlist[i]);
            }
            return(string.Format("\"{0}\" : \"{1}\",\n", sName, sWrite));
        }

        int[] intlist = content as int[];
        if (null != intlist)
        {
            string sWrite = "";
            for (int i = 0; i < intlist.Length; ++i)
            {
                sWrite += (0 == i) ? intlist[i].ToString() : (";" + intlist[i]);
            }
            return(string.Format("\"{0}\" : \"{1}\",\n", sName, sWrite));
        }

        float[] fltlist = content as float[];
        if (null != fltlist)
        {
            string sWrite = "";
            for (int i = 0; i < fltlist.Length; ++i)
            {
                sWrite += (0 == i) ? GetFloatTo4(fltlist[i]) : (";" + GetFloatTo4(fltlist[i]));
            }
            return(string.Format("\"{0}\" : \"{1}\",\n", sName, sWrite));
        }

        CMGDataElement[] cmlist = content as CMGDataElement[];
        if (null != cmlist)
        {
            string sWrite = "";
            foreach (CMGDataElement t in cmlist)
            {
                if (null != t)
                {
                    sWrite += (string.IsNullOrEmpty(sWrite)) ? t.GetString() : (";" + t.GetString());
                }
            }
            return(string.Format("\"{0}\" : \"{1}\",\n", sName, sWrite));
        }

        CMGDataElement element = content as CMGDataElement;

        if (null != element)
        {
            return(string.Format("\"{0}\" : \"{1}\",\n", sName, element.GetString()));
        }

        if (content is Color)
        {
            return(string.Format("\"{0}\" : \"{1},{2},{3},{4}\",\n",
                                 sName,
                                 Mathf.Clamp(Mathf.RoundToInt(((Color)content).r * 255.0f), 0, 255),
                                 Mathf.Clamp(Mathf.RoundToInt(((Color)content).g * 255.0f), 0, 255),
                                 Mathf.Clamp(Mathf.RoundToInt(((Color)content).b * 255.0f), 0, 255),
                                 Mathf.Clamp(Mathf.RoundToInt(((Color)content).a * 255.0f), 0, 255)
                                 ));
        }
        if (content is Vector2)
        {
            return(string.Format("\"{0}\" : \"{1},{2}\",\n",
                                 sName,
                                 GetFloatTo4(((Vector2)content).x),
                                 GetFloatTo4(((Vector2)content).y)
                                 ));
        }
        if (content is Vector3)
        {
            return(string.Format("\"{0}\" : \"{1},{2},{3}\",\n",
                                 sName,
                                 GetFloatTo4(((Vector3)content).x),
                                 GetFloatTo4(((Vector3)content).y),
                                 GetFloatTo4(((Vector3)content).z)
                                 ));
        }
        if (content is Vector4)
        {
            return(string.Format("\"{0}\" : \"{1},{2},{3},{4}\",\n",
                                 sName,
                                 GetFloatTo4(((Vector4)content).x),
                                 GetFloatTo4(((Vector4)content).y),
                                 GetFloatTo4(((Vector4)content).z),
                                 GetFloatTo4(((Vector4)content).w)
                                 ));
        }
        if (content is float)
        {
            return(string.Format("\"{0}\" : \"{1}\",\n", sName, GetFloatTo4((float)content)));
        }

        return(string.Format("\"{0}\" : \"{1}\",\n", sName, content));
    }
예제 #2
0
    protected object GetElementValue(string sContent, string sName, object defaultValue)
    {
        string          sFormat = string.Format("\\\"{0}\\\"[\\s]*\\:[\\s]*\\\"[^\\\"]+\\\"[\\s]*", sName);
        string          sFound  = null;
        MatchCollection matches = Regex.Matches(sContent, sFormat);

        if (matches.Count > 0)
        {
            int iIndex1 = matches[0].Value.IndexOf('\"');
            int iIndex2 = matches[0].Value.IndexOf('\"', iIndex1 + 1);
            int iIndex3 = matches[0].Value.IndexOf('\"', iIndex2 + 1);
            int iIndex4 = matches[0].Value.IndexOf('\"', iIndex3 + 1);
            if (iIndex3 > 0 && iIndex4 > iIndex3)
            {
                sFound = matches[0].Value.Substring(iIndex3 + 1, iIndex4 - iIndex3 - 1);
            }
        }

        if (string.IsNullOrEmpty(sFound))
        {
            return(defaultValue);
        }

        #region PrimeTypes

        if (defaultValue is string)
        {
            return(sFound);
        }
        if (defaultValue is int)
        {
            int ret;
            if (int.TryParse(sFound, out ret))
            {
                return(ret);
            }
        }
        if (defaultValue is sint)
        {
            int ret;
            if (int.TryParse(sFound, out ret))
            {
                return((sint)ret);
            }
        }
        if (defaultValue is uint)
        {
            uint ret;
            if (uint.TryParse(sFound, out ret))
            {
                return(ret);
            }
        }
        if (defaultValue is long)
        {
            long ret;
            if (long.TryParse(sFound, out ret))
            {
                return(ret);
            }
        }
        if (defaultValue is ulong)
        {
            ulong ret;
            if (ulong.TryParse(sFound, out ret))
            {
                return(ret);
            }
        }
        if (defaultValue is short)
        {
            short ret;
            if (short.TryParse(sFound, out ret))
            {
                return(ret);
            }
        }
        if (defaultValue is ushort)
        {
            ushort ret;
            if (ushort.TryParse(sFound, out ret))
            {
                return(ret);
            }
        }
        if (defaultValue is byte)
        {
            byte ret;
            if (byte.TryParse(sFound, out ret))
            {
                return(ret);
            }
        }
        if (defaultValue is float)
        {
            float ret;
            if (float.TryParse(sFound, out ret))
            {
                return(ret);
            }
        }
        if (defaultValue is sfloat)
        {
            float ret;
            if (float.TryParse(sFound, out ret))
            {
                return((sfloat)ret);
            }
        }
        if (defaultValue is bool)
        {
            bool ret;
            if (bool.TryParse(sFound, out ret))
            {
                return(ret);
            }
        }
        if (defaultValue is sbool)
        {
            bool ret;
            if (bool.TryParse(sFound, out ret))
            {
                return((sbool)ret);
            }
        }
        if (defaultValue is Vector2)
        {
            string[] args = sFound.Split(',');
            float    fx, fy;
            if (2 == args.Length && float.TryParse(args[0], out fx) && float.TryParse(args[1], out fy))
            {
                return(new Vector2(fx, fy));
            }
        }
        if (defaultValue is Vector3)
        {
            string[] args = sFound.Split(',');
            float    fx, fy, fz;
            if (3 == args.Length && float.TryParse(args[0], out fx) && float.TryParse(args[1], out fy) && float.TryParse(args[2], out fz))
            {
                return(new Vector3(fx, fy, fz));
            }
        }
        if (defaultValue is Vector4)
        {
            string[] args = sFound.Split(',');
            float    fx, fy, fz, fw;
            if (4 == args.Length && float.TryParse(args[0], out fx) && float.TryParse(args[1], out fy) && float.TryParse(args[2], out fz) && float.TryParse(args[3], out fw))
            {
                return(new Vector4(fx, fy, fz, fw));
            }
        }
        if (defaultValue is Color)
        {
            string[] args = sFound.Split(',');
            byte     fr, fg, fb, fa;
            if (4 == args.Length && byte.TryParse(args[0], out fr) && byte.TryParse(args[1], out fg) && byte.TryParse(args[2], out fb) && byte.TryParse(args[3], out fa))
            {
                return(new Color(fr / 255.0f, fg / 255.0f, fb / 255.0f, fa / 255.0f));
            }
        }
        if (defaultValue is Enum)
        {
            return(Enum.Parse(defaultValue.GetType(), sFound));
        }

        #endregion

        #region Lists

        if (defaultValue is string[])
        {
            return(sFound.Split(';'));
        }
        if (defaultValue is int[])
        {
            List <int> intlist = new List <int>();
            foreach (string sInner in sFound.Split(';'))
            {
                int ielement;
                if (!string.IsNullOrEmpty(sInner) && int.TryParse(sInner, out ielement))
                {
                    intlist.Add(ielement);
                }
            }
            return(intlist.ToArray());
        }
        if (defaultValue is float[])
        {
            List <float> intlist = new List <float>();
            foreach (string sInner in sFound.Split(';'))
            {
                float ielement;
                if (!string.IsNullOrEmpty(sInner) && float.TryParse(sInner, out ielement))
                {
                    intlist.Add(ielement);
                }
            }
            return(intlist.ToArray());
        }

        #endregion

        #region Child Data

        CMGDataElement defC = defaultValue as CMGDataElement;
        if (null != defC)
        {
            defC.LoadData(sFound);
            return(defC);
        }

        CMGDataElement[] defLst = defaultValue as CMGDataElement[];
        if (null != defLst && defLst.Length > 0 && null != defLst[0])
        {
            string[] args = sFound.Split(';');
            defLst = new CMGDataElement[args.Length];
            for (int i = 0; i < args.Length; ++i)
            {
                CMGDataElement newElement = defLst[0].GetDefault();
                newElement.LoadData(args[i]);
                defLst[i] = newElement;
            }
            return(defLst);
        }

        #endregion

        return(defaultValue);
    }
예제 #3
0
    public static object EditorField(string sDesc, object current, int iLineSpace, ref bool bInBeginLine)
    {
        #region Prime Types

        if (current is int)
        {
            return(EditorGUILayout.IntField(sDesc, (int)current));
        }
        if (current is sint)
        {
            return((sint)EditorGUILayout.IntField(sDesc, (sint)current));
        }
        if (current is short)
        {
            return((short)EditorGUILayout.IntField(sDesc, (short)current));
        }
        if (current is ushort)
        {
            return((ushort)EditorGUILayout.IntField(sDesc, (ushort)current));
        }
        if (current is byte)
        {
            return((byte)EditorGUILayout.IntField(sDesc, (byte)current));
        }
        if (current is long)
        {
            return(EditorGUILayout.LongField(sDesc, (long)current));
        }

        if (current is float)
        {
            return(EditorGUILayout.FloatField(sDesc, (float)current));
        }
        if (current is sfloat)
        {
            return((sfloat)EditorGUILayout.FloatField(sDesc, (sfloat)current));
        }

        if (current is bool)
        {
            return(EditorGUILayout.Toggle(sDesc, (bool)current));
        }
        if (current is sbool)
        {
            return((sbool)EditorGUILayout.Toggle(sDesc, (sbool)current));
        }

        string sCurrent = current as string;
        if (null != sCurrent)
        {
            return(EditorGUILayout.TextField(sDesc, sCurrent));
        }

        //=====================================================================
        //Bit Field
        if (current is uint)
        {
            return((uint)EditorGUILayout.LongField(sDesc, (uint)current));
        }
        if (current is ulong)
        {
            return((ulong)EditorGUILayout.LongField(sDesc, (long)(ulong)current));
        }

        //=====================================================================
        //Struct Field
        if (current is Vector2)
        {
            return(EditorGUILayout.Vector2Field(sDesc, (Vector2)current));
        }
        if (current is Vector3)
        {
            return(EditorGUILayout.Vector3Field(sDesc, (Vector3)current));
        }
        if (current is Vector4)
        {
            return(EditorGUILayout.Vector4Field(sDesc, (Vector4)current));
        }
        if (current is Color)
        {
            return(EditorGUILayout.ColorField(sDesc, (Color)current));
        }
        Enum eCurrent = current as Enum;
        if (null != eCurrent)
        {
            return(EditorGUILayout.EnumPopup(sDesc, eCurrent));
        }

        #endregion

        #region List

        //=====================================================================
        //String List Field
        string[] sCurrentList = current as string[];
        if (null != sCurrentList)
        {
            string sCurrentV = sCurrentList.Aggregate("", (current1, eachone) => current1 + (string.IsNullOrEmpty(current1) ? eachone : (";" + eachone)));
            sCurrentV = EditorGUILayout.TextField(sDesc, sCurrentV);
            return(sCurrentV.Split(';'));
        }

        int[] iCurrentList = current as int[];
        if (null != iCurrentList)
        {
            BeginLine(iLineSpace, ref bInBeginLine);
            int length = iCurrentList.Length;
            length = EditorGUILayout.IntField("长度", length);
            EndLine(ref bInBeginLine);
            BeginLine(iLineSpace, ref bInBeginLine);
            List <int> res = new List <int>();
            for (int i = 0; i < length; ++i)
            {
                if (0 != i && 0 == (i % 4))
                {
                    EndLine(ref bInBeginLine);
                    BeginLine(iLineSpace, ref bInBeginLine);
                }
                int ioneres = EditorGUILayout.IntField(i + ":", i < iCurrentList.Length ? iCurrentList[i] : 0);
                res.Add(ioneres);
            }
            EndLine(ref bInBeginLine);
            return(res.ToArray());
        }

        float[] fCurrentList = current as float[];
        if (null != fCurrentList)
        {
            BeginLine(iLineSpace, ref bInBeginLine);
            int length = fCurrentList.Length;
            length = EditorGUILayout.IntField("长度", length);
            EndLine(ref bInBeginLine);
            BeginLine(iLineSpace, ref bInBeginLine);
            List <float> res = new List <float>();
            for (int i = 0; i < length; ++i)
            {
                if (0 != i && 0 == (i % 4))
                {
                    EndLine(ref bInBeginLine);
                    BeginLine(iLineSpace, ref bInBeginLine);
                }
                float foneres = EditorGUILayout.FloatField(i + ":", i < fCurrentList.Length ? fCurrentList[i] : 0.0f);
                res.Add(foneres);
            }
            EndLine(ref bInBeginLine);
            return(res.ToArray());
        }

        #endregion

        #region MGAData

        CMGDataElement dataElement = current as CMGDataElement;
        if (dataElement != null)
        {
            BeginLine(iLineSpace, ref bInBeginLine);
            bool bFold = (0 == dataElement.m_iID);
            bFold             = EditorGUILayout.Foldout(bFold, sDesc + ":");
            dataElement.m_iID = bFold ? 0 : 1;
            EndLine(ref bInBeginLine);
            if (bFold && null != m_pElementEditors && m_pElementEditors.ContainsKey(dataElement.GetType()))
            {
                m_pElementEditors[dataElement.GetType()].Invoke(null, new[] { current, iLineSpace + 2 });
            }
        }

        CMGDataElement[] dataElements = current as CMGDataElement[];
        if (dataElements != null)
        {
            BeginLine(iLineSpace, ref bInBeginLine);
            GUILayout.Label(sDesc + ":");
            EndLine(ref bInBeginLine);
            Type elementType = dataElements.GetType().GetElementType();
            List <CMGDataElement> realData = dataElements.ToList();
            if (null != elementType)
            {
                for (int i = 0; i < realData.Count; ++i)
                {
                    if (null != m_pElementEditors && m_pElementEditors.ContainsKey(elementType))
                    {
                        if (null == realData[i])
                        {
                            realData[i] = (CMGDataElement)Activator.CreateInstance(elementType);
                        }

                        BeginLine(iLineSpace + 1, ref bInBeginLine);
                        bool bFold = (0 == realData[i].m_iID);
                        realData[i].m_iIndex = i;
                        bFold             = EditorGUILayout.Foldout(bFold, sDesc + (i + 1) + ":");
                        realData[i].m_iID = bFold ? 0 : 1;
                        EndLine(ref bInBeginLine);

                        if (bFold)
                        {
                            m_pElementEditors[elementType].Invoke(null, new object[] { realData[i], iLineSpace + 2 });

                            BeginLine(iLineSpace + 2, ref bInBeginLine);
                            if (GUILayout.Button("删除" + sDesc + (i + 1)))
                            {
                                realData[i] = null;
                            }
                            EndLine(ref bInBeginLine);
                        }
                    }
                }
                BeginLine(iLineSpace, ref bInBeginLine);
                if (GUILayout.Button("添加" + sDesc))
                {
                    realData.Add((CMGDataElement)Activator.CreateInstance(elementType));
                }
                EndLine(ref bInBeginLine);
                realData.RemoveAll(oneele => null == oneele);

                CMGDataElement[] retarrary = (CMGDataElement[])Array.CreateInstance(elementType, realData.Count);
                for (int i = 0; i < realData.Count; ++i)
                {
                    retarrary[i] = realData[i];
                }

                return(retarrary);
            }
            return(current);
        }

        #endregion

        return(null);
    }