void Start()
        {
            ClassA     classA = new ClassA();
            JSONObject json   = classA.ToJsonObject();

            VeerDebug.Log(json.ToString());
        }
Пример #2
0
    private static ClassTypeToJsonFunction CreateTypeSerializeFunctionForDictionary(Type targetType)
    {
        // 1.check type
        if (!targetType.IsGenericType || targetType.GetGenericTypeDefinition() != typeof(Dictionary <,>))
        {
            VeerDebug.LogWarning(" CreateTypeSerializeFunctionForDictionary but target type is not dictionary : " + targetType);
            return(null);
        }

        // 2.check dictionary key type is string
        Type keyType = targetType.GetGenericArguments()[0];

        if (keyType != typeof(string))
        {
            VeerDebug.LogWarning(" ToJsonObject 不支持 KeyType 不是 string 的 Dictionary : " + targetType);
            return(null);
        }

        // 3.check register value type
        Type dicValueType = targetType.GetGenericArguments()[1];

        CheckRegister(dicValueType);

        // 4.create function
        ClassTypeToJsonFunction function = (object dictionaryObj) =>
        {
            JSONObject jsonDictionary = JSONObject.obj;

            if (dictionaryObj == null)
            {
                return(jsonDictionary);
            }

            IDictionary           iDictionary = (IDictionary)dictionaryObj;
            IDictionaryEnumerator enumerator  = iDictionary.GetEnumerator();

            if (_BasicTypeToJsonFunctionMap.ContainsKey(dicValueType))
            {
                while (enumerator.MoveNext())
                {
                    _BasicTypeToJsonFunctionMap[dicValueType](jsonDictionary, enumerator.Key.ToString(), enumerator.Value);
                }
            }
            else if (_ClassTypeToJsonFunctionMap.ContainsKey(dicValueType))
            {
                ClassTypeToJsonFunction toJsonFunc = _ClassTypeToJsonFunctionMap[dicValueType];
                if (toJsonFunc != null)
                {
                    while (enumerator.MoveNext())
                    {
                        jsonDictionary.AddField(enumerator.Key.ToString(), toJsonFunc(enumerator.Value));
                    }
                }
            }

            return(jsonDictionary);
        };

        return(function);
    }
Пример #3
0
    public static void RegisterClassTypeSerializeFunction(Type classType, ClassDefaultResetFunc classDefaultResetFunc, bool bOverride = false)
    {
        if (classType == null || !classType.IsClass)
        {
#if UNITY_EDITOR
            VeerDebug.LogWarning(" RegisterTypeSerializeFunction Type 为 null 或不是 Class ...");
#endif
            return;
        }

        if (bOverride)
        {
            _ClassDefaultResetFuncMap.SetAddValue(classType, classDefaultResetFunc);
        }
        else
        {
            if (_ClassDefaultResetFuncMap.ContainsKey(classType))
            {
                return;
            }
            else
            {
                _ClassDefaultResetFuncMap.Add(classType, classDefaultResetFunc);
            }
        }
    }
Пример #4
0
 private void TestTweenWapper()
 {
     if ((GUILayout.Button("Create TweenWarpper")))
     {
         TweenWarpper = new TweenWarpper();
     }
     if ((GUILayout.Button("Play Tween")))
     {
         TestFloat = 0;
         TweenWarpper.Set(DOTween
                          .To(() => { return(TestFloat); }, (f) => { TestFloat = f; }, 100, 5)
                          .OnComplete(() => { VeerDebug.Log("tween complete ..."); })
                          .OnKill(() => { VeerDebug.Log("tween kill ..."); }));
     }
     if ((GUILayout.Button("Just Kill")))
     {
         TweenWarpper.Abort(AbortMethod.JustKill);
     }
     if ((GUILayout.Button("Force With OnKill")))
     {
         TweenWarpper.Abort(AbortMethod.ForceCompleteWithKillCallback);
     }
     if ((GUILayout.Button("Force With OnComplete")))
     {
         TweenWarpper.Abort(AbortMethod.ForceCompleteWithOnCompleteCallback);
     }
     if ((GUILayout.Button("Force With OnKill and OnComplete")))
     {
         TweenWarpper.Abort(AbortMethod.ForceCompleteWithOnCompleteAndOnKill);
     }
 }
Пример #5
0
    // 测试开启 多线程 Abort
    public void Test_TaskAbort()
    {
        List <Task> taskList = new List <Task>();

        TaskListChecker  = new List <string>();
        TaskCountChecker = 500;
        for (int i = 1; i < 501; i++)
        {
            string index = i.ToString();
            TaskListChecker.Add(index);
            taskList.Add(Task <string> .Run(() =>
            {
                SomeCalculateSimple();
                return(index);
            }).ContinueInMainThreadWith((obj) =>
            {
                TaskCountChecker--;

                if (obj == null)
                {
                    VeerDebug.Log(" time out : " + index);
                    return;
                }

                VeerDebug.Log(" thread over : " + obj.Result);
                TaskListChecker.Remove(obj.Result);
            }, 10f));
        }

        foreach (var task in taskList)
        {
            task.CheckAbort();
        }
    }
Пример #6
0
    private static ClassTypeToJsonFunction CreateTypeSerializeFunction(Type targetType)
    {
        IsCreatingFuncTypeList.Add(targetType);

        ClassTypeToJsonFunction toJsonObjectFunction = null;

        if (targetType.IsEnum)
        {
#if UNITY_EDITOR
            VeerDebug.LogWarning(" 目前不支持 Enum 类型 ToJsonObject ...");
#endif
            toJsonObjectFunction = null;
        }
        else if (targetType.IsArray || targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List <>))
        {
            toJsonObjectFunction = CreateTypeSerializeFunctionForArrayList(targetType);
        }
        else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(Dictionary <,>))
        {
            toJsonObjectFunction = CreateTypeSerializeFunctionForDictionary(targetType);
        }
        else if (targetType.IsClass)
        {
            toJsonObjectFunction = CreateTypeSerializeFunctionForClass(targetType);
        }

        // 注册方法
        RegisterClassTypeSerializeFunction(targetType, toJsonObjectFunction);

        IsCreatingFuncTypeList.Remove(targetType);

        return(toJsonObjectFunction);
    }
Пример #7
0
    private static ClassTypeToJsonFunction CreateTypeSerializeFunctionForArrayList(Type targetType)
    {
        // 1.check type
        Type arrayListElementType = null;

        if (targetType.IsArray)
        {
            arrayListElementType = targetType.GetElementType();
        }
        else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List <>))
        {
            arrayListElementType = targetType.GetGenericArguments()[0];
        }
        if (arrayListElementType == null)
        {
            VeerDebug.LogWarning(" CreateTypeSerializeFunctionForArrayList but target type is not arrar or list : " + targetType);
            return(null);
        }

        // 2.check register element type
        CheckRegister(arrayListElementType);

        // 3.create function
        ClassTypeToJsonFunction function = (object arrayListObj) =>
        {
            JSONObject jsonArray = JSONObject.arr;

            if (arrayListObj == null)
            {
                return(jsonArray);
            }

            IEnumerable elementArray = arrayListObj as IEnumerable;

            if (_BasicTypeToJsonFunctionMap.ContainsKey(arrayListElementType))
            {
                foreach (var element in elementArray)
                {
                    _BasicTypeToJsonFunctionMap[arrayListElementType](jsonArray, null, element);
                }
            }
            else if (_ClassTypeToJsonFunctionMap.ContainsKey(arrayListElementType))
            {
                ClassTypeToJsonFunction toJsonFunc = _ClassTypeToJsonFunctionMap[arrayListElementType];
                if (toJsonFunc != null)
                {
                    foreach (var element in elementArray)
                    {
                        jsonArray.Add(toJsonFunc(element));
                    }
                }
            }

            return(jsonArray);
        };

        return(function);
    }
Пример #8
0
    /// <summary>
    /// 解析形如 "0.000601,7.072153,0,YXZ" 的旋转信息
    /// </summary>
    public static Quaternion ParseWebOrientationString(string veerWebOrientationString, bool bIgnoreX = false, bool bIgnoreY = false, bool bIgnoreZ = false)
    {
        Quaternion result = Quaternion.identity;

        if (string.IsNullOrEmpty(veerWebOrientationString))
        {
            return(result);
        }

        try
        {
            string[] str = veerWebOrientationString.Split(new char[] { ',' });
            if (str.Length == 4)
            {
                string     k = str[3].ToLower();
                Quaternion q = Quaternion.identity;
                Quaternion x = Quaternion.identity;
                Quaternion y = Quaternion.identity;
                Quaternion z = Quaternion.identity;

                x = Quaternion.AngleAxis(-float.Parse(str[0]) * Mathf.Rad2Deg, Vector3.right);
                y = Quaternion.AngleAxis(-float.Parse(str[1]) * Mathf.Rad2Deg, Vector3.up);
                z = Quaternion.AngleAxis(float.Parse(str[2]) * Mathf.Rad2Deg, Vector3.forward);

                for (int i = 0; i < 3; i++)
                {
                    if (k[i] == 'x' && !bIgnoreX)
                    {
                        q *= x;
                    }
                    else if (k[i] == 'y' && !bIgnoreY)
                    {
                        q *= y;
                    }
                    else if (k[i] == 'z' && !bIgnoreZ)
                    {
                        q *= z;
                    }
                }

                // songlingyi
                // Web 端设置 Setting 初始角度 时 使用的是 Camera 方向
                // 而 VR 端使用球的旋转来设
                // 因此此处要对 Quaternion 取反
                result = Quaternion.Inverse(q);
            }
        }
        catch (System.Exception e)
        {
            VeerDebug.Log(e.Message);
            VeerDebug.Log(e.StackTrace);
            return(Quaternion.identity);
        }

        return(result);
    }
Пример #9
0
    // 核心方法
    private static void DefaultReset_Internal(object obj)
    {
        if (!_Inited)
        {
            Init();
        }

        // 检查空值
        if (obj == null)
        {
            return;
        }

        Type targetType = null;

        // 检查是否为 Type 类型
        if (obj is Type)
        {
            targetType = obj as Type;

            // How to tell if a Type is a static class?
            // https://stackoverflow.com/questions/4145072/how-to-tell-if-a-type-is-a-static-class
            if (!targetType.IsClass || !targetType.IsAbstract || !targetType.IsSealed)
            {
                VeerDebug.LogWarning(" 不能对 非静态类 进行 DefaultReset_Internal(Type): " + targetType.Name);
                return;
            }
        }
        else
        {
            // 检查是否为 类的实例
            targetType = obj.GetType();
            if (!targetType.IsClass)
            {
                VeerDebug.LogWarning(" 不支持对非类的实例进行 Default Reset 操作 : " + targetType.Name);
                return;
            }
        }

        CheckRegister(targetType);

        // 检查是否已有 DefaultReset Func
        if (_ClassDefaultResetFuncMap.ContainsKey(targetType))
        {
            ClassDefaultResetFunc existingFunction = _ClassDefaultResetFuncMap[targetType];
            if (existingFunction == null)
            {
                VeerDebug.LogWarning(" 对无法支持的类型进行 DefaultReset 操作 : " + targetType.Name);
                return;
            }

            // 使用已有方法进行 DefaultReset
            existingFunction(obj);
        }
    }
Пример #10
0
    public ObjectBinderAttribute(string objectName, bool nullable = false)
    {
#if UNITY_EDITOR
        if (!objectName.StartsWith(_StartWith))
        {
            VeerDebug.LogError(" ObjectBinderAttribute objectName not start with " + _StartWith);
        }
#endif

        ObjectName = objectName;
        bNullable  = nullable;
    }
Пример #11
0
    public void RegisterDynamicWidthUnit(DynamicWidthUnitBase unit)
    {
#if UNITY_EDITOR
        if (_UnitList.Contains(unit))
        {
            VeerDebug.LogError(" try to register a existing DynamicWidthUnitBase...");
            return;
        }
#endif
        _UnitList.Add(unit);
        unit.transform.SetParent(UnitRoot);
    }
Пример #12
0
 // 测试 Task.RunInMainThread 没什么用,正常阻塞
 public void Test_RunInMainThread()
 {
     // executed in the main thread
     VeerDebug.Log(" Test_RunInMainThread Start ...");
     Task.RunInMainThread(() =>
     {
         VeerDebug.Log(" Test_RunInMainThread ...");
         GameObject g = new GameObject("_auto_Test_RunInMainThread");
         int rst      = SomeCalculate();
     });
     VeerDebug.Log(" Test_RunInMainThread End ...");
 }
Пример #13
0
    public void Test_InitVeerDebug()
    {
        VeerDebugHelper.Instance.InitDebug();

        VeerDebug.SetDebugLogMode(VeerDebug.LogMode.ForbidTagMode);

        VeerDebug.AddStandaloneCacheTag("TagA");
        //VeerDebug.AddStandaloneCacheTag("TagB");
        VeerDebug.AddStandaloneCacheTag("TagC");
        VeerDebug.AddStandaloneCacheTag("TagD");

        VeerDebug.AddForbidTag("TagD");
    }
Пример #14
0
 // 测试 Task.Run
 public void Test_Run()
 {
     // executed in the new thread
     // log 的 次序是不确定的,注意 异步编程 / 多线程编程 的常见问题
     VeerDebug.Log(" Test_Run Start ...");
     Task.Run(() =>
     {
         VeerDebug.Log(" Test_Run ...");
         // 在 Run 中操作 UnityEngine.Object 子类会引发 Crash
         //GameObject g = new GameObject("_auto_Test_Run");
     });
     VeerDebug.Log(" Test_Run End ...");
 }
Пример #15
0
 public void Test_Debug()
 {
     VeerDebug.Log(GetRandomString());
     VeerDebug.Log(GetRandomString());
     VeerDebug.Log("TagA", GetRandomString());
     VeerDebug.Log("TagA", GetRandomString());
     VeerDebug.Log("TagB", GetRandomString());
     VeerDebug.Log("TagB", GetRandomString());
     VeerDebug.Log("TagC", GetRandomString());
     VeerDebug.Log("TagC", GetRandomString());
     VeerDebug.Log("TagD", GetRandomString());
     VeerDebug.Log("TagD", GetRandomString());
 }
Пример #16
0
 // 测试 Task.Run 返回后执行 Main Thread 回调方法
 public void Test_RunIntFunc()
 {
     Task <int> .Run(() =>
     {
         int rst = SomeCalculate();
         VeerDebug.Log(" in another thread rst : " + rst);
         return(rst);
     }).ContinueInMainThreadWith((obj) =>
     {
         VeerDebug.Log(" in main thread rst : " + obj.Result);
         GameObject g = new GameObject("_auto_Test_RunIntFunc");
     });
 }
Пример #17
0
    private void Test_7_JsonObjectCreate()
    {
        JSONObject json = JSONObject.obj;

        json.AddField("StringField", TestInnerJsonText);

        string jsonString = json.ToString();

        VeerDebug.Log("json to string : " + jsonString);

        JSONObject jsonParse = JSONObject.Create(jsonString);

        VeerDebug.Log("json parsed : " + jsonParse.ToString());
    }
Пример #18
0
    private void Test_8_FastJsonMyText()
    {
        string jsonString = "{\"StringField\":\"ha\\\"ha\\\"ha\"}";

        VeerDebug.Log("json string : " + jsonString);

        TestJsonConvert test = FastJson.Deserialize <TestJsonConvert>(jsonString);

        VeerDebug.Log("fast json string field : " + test.StringField);

        string serialize = FastJson.Serialize(test);

        VeerDebug.Log("fast json serialize : " + serialize);
    }
Пример #19
0
    public static void SetTextConfig(Text targetText)
    {
#if UNITY_EDITOR
        if (targetText == null)
        {
            VeerDebug.LogError(" SetTextConfig targetText is null...");
        }
#endif
        Instance.TestText.font            = targetText.font;
        Instance.TestText.fontSize        = targetText.fontSize;
        Instance.TestText.fontStyle       = targetText.fontStyle;
        Instance.TestText.lineSpacing     = targetText.lineSpacing;
        Instance.TestText.supportRichText = targetText.supportRichText;
        Instance.TestText.alignment       = targetText.alignment;
        Instance.TestText.alignByGeometry = targetText.alignByGeometry;
    }
Пример #20
0
    private static void CheckRegister(Type targetType)
    {
        if (_ClassDefaultResetFuncMap.ContainsKey(targetType))
        {
            return;
        }

        CreateTypeDefaultResetFunc(targetType);

#if UNITY_EDITOR
        if (!_ClassDefaultResetFuncMap.ContainsKey(targetType))
        {
            VeerDebug.LogWarning(" ClassDefaultResetFunc 不支持该类型的 DefaultReset 方法 : " + targetType);
        }
#endif
    }
Пример #21
0
        public void Test_DefReset()
        {
            VeerDebug.Log(StaticDefaultReset.StaticStringField);
            ObjectDefaultReset.DefaultReset(typeof(StaticDefaultReset));
            VeerDebug.Log(StaticDefaultReset.StaticStringField);

            NoStaticDefaultReset inst = new NoStaticDefaultReset();

            VeerDebug.Log(NoStaticDefaultReset.StaticStringField);
            VeerDebug.Log(inst.StringField);
            VeerDebug.Log(inst.IntField);
            inst.DefaultReset();
            VeerDebug.Log(NoStaticDefaultReset.StaticStringField);
            VeerDebug.Log(inst.StringField);
            VeerDebug.Log(inst.IntField);
        }
Пример #22
0
    private void Test_5_OriginalEqualUnescape()
    {
        string ori = TestInnerJsonText;
        string une = TestEscapeUnescapeJsonText;

        VeerDebug.Log("Original Unescape Length : " + TestInnerJsonText.Length + "  " + TestEscapeUnescapeJsonText.Length);

        for (int i = 0; i < ori.Length; i++)
        {
            if (ori[i] != une[i])
            {
                VeerDebug.Log(i.ToString() + " : " + ori[i] + " " + une[i]);
            }
        }

        VeerDebug.Log("OriginalEqualUnescape : " + (TestInnerJsonText == TestEscapeUnescapeJsonText).ToString());
    }
Пример #23
0
    private static void CheckRegister(Type targetType)
    {
        if (_BasicTypeToJsonFunctionMap.ContainsKey(targetType) ||
            _ClassTypeToJsonFunctionMap.ContainsKey(targetType))
        {
            return;
        }

        CreateTypeSerializeFunction(targetType);

#if UNITY_EDITOR
        if (!_ClassTypeToJsonFunctionMap.ContainsKey(targetType))
        {
            VeerDebug.LogWarning(" CreateTypeSerializeFunction 不支持该类型的 ToJsonObject 方法 : " + targetType);
        }
#endif
    }
Пример #24
0
    public static Task AsyncCreate(string jsonText, JSONObjectAsyncCreateCallback callback)
    {
#if UNITY_EDITOR
        if (callback == null)
        {
            VeerDebug.LogError(" JSONObject AsyncCreate callback is null...");
            return(null);
        }
#endif

        Task task = Task <JSONObject> .Run(() =>
        {
            JSONObject json = null;
            try
            {
                json = Create(jsonText);
            }
            catch
            {
                VeerDebug.LogWarning(" create json failed ...");
                return(null);
            }
            return(json);
        }).ContinueInMainThreadWith((obj) =>
        {
            if (obj == null)
            {
                VeerDebug.LogWarning(" async json object create timeout, maybe lost thread ...");
                callback(false, null);
                return;
            }
            else if (obj.IsFaulted || obj.IsAborted || !obj.IsCompleted)
            {
                VeerDebug.LogWarning(" async json object create timeout, maybe lost thread ...");
                callback(false, null);
                return;
            }
            else
            {
                callback(true, obj.Result);
                return;
            }
        }, 8f);

        return(task);
    }
Пример #25
0
    private void Test_9_FastJsonSerialize()
    {
        JSONObject json = JSONObject.obj;

        json.AddField("StringField", TestInnerJsonText);

        string jsonString = json.ToString();

        VeerDebug.Log("json to string : " + jsonString);

        TestJsonConvert test = FastJson.Deserialize <TestJsonConvert>(jsonString);

        VeerDebug.Log("fast json string field : " + test.StringField);

        string serialize = FastJson.Serialize(test);

        VeerDebug.Log("fast json serialize : " + serialize);
    }
Пример #26
0
 private void TestOriginTween()
 {
     if ((GUILayout.Button("Check Tween")))
     {
         if (Tween1 == null)
         {
             VeerDebug.Log("tween is null ...");
         }
         else
         {
             VeerDebug.Log("tween is exist ...");
         }
     }
     if ((GUILayout.Button("Create Tween 1")))
     {
         TestFloat = 0;
         Tween1    = DOTween.To(() => { return(TestFloat); }, (f) => { TestFloat = f; }, 100, 5)
                     .OnComplete(() => { VeerDebug.Log("tween complete ..."); })
                     .Pause();
     }
     if ((GUILayout.Button("Create Tween 2")))
     {
         TestFloat = 0;
         Tween2    = DOTween.To(() => { return(TestFloat); }, (f) => { TestFloat = f; }, 100, 5)
                     .OnComplete(() => { VeerDebug.Log("tween complete ..."); })
                     .Pause();
     }
     if ((GUILayout.Button("Play Tween 1")))
     {
         Tween1.Play();
     }
     if ((GUILayout.Button("Play Tween 2")))
     {
         Tween2.Play();
     }
     if ((GUILayout.Button("Complete Tween 1")))
     {
         Tween1.Complete();
     }
     if ((GUILayout.Button("Kill Tween 1")))
     {
         Tween1.Kill();
     }
 }
Пример #27
0
    // 核心方法
    private static JSONObject ToJsonObject_Internal(object target)
    {
        if (!_Inited)
        {
            Init();
        }

        // 检查空值
        if (target == null)
        {
            return(null);
        }

        // 检查是否为 类的实例
        Type targetType = target.GetType();

        if (!targetType.IsClass)
        {
            VeerDebug.LogWarning(" 不支持对非类的实例进行 ToJsonObject 操作 : " + targetType.Name);
            return(null);
        }

        // register
        CheckRegister(targetType);

        // 检查是否已有 ToJson方法
        if (_ClassTypeToJsonFunctionMap.ContainsKey(targetType))
        {
            ClassTypeToJsonFunction existingFunction = _ClassTypeToJsonFunctionMap[targetType];
            if (existingFunction == null)
            {
                VeerDebug.LogWarning(" 对无法支持的类型进行 ToJsonObject 操作 : " + targetType.Name);
                return(null);
            }
            // 使用已有方法进行 ToJson
            return(_ClassTypeToJsonFunctionMap[targetType](target));
        }
        else
        {
            return(null);
        }
    }
Пример #28
0
    public static Task AsyncDeserializeJson <T>(string jsonText, Action <bool, T> callback, float timeout = 6f)
    {
#if UNITY_EDITOR
        if (callback == null)
        {
            VeerDebug.LogError(" AsyncDeserializeJson callback is null...");
            return(null);
        }
#endif

        Task task = Task <T> .Run(() =>
        {
            return(Deserialize <T>(jsonText));
        }).ContinueInMainThreadWith((obj) =>
        {
            AsyncDeserializeJsonCallback(callback, obj);
        }, timeout);

        return(task);
    }
Пример #29
0
    public static Task AsyncDeserializeJson(JSONObject json, Type type, Action <bool, object> callback, float timeout = 6f)
    {
#if UNITY_EDITOR
        if (callback == null)
        {
            VeerDebug.LogError(" AsyncDeserializeJson callback is null...");
            return(null);
        }
#endif

        Task task = Task <object> .Run(() =>
        {
            return(Deserialize(json, type));
        }).ContinueInMainThreadWith((obj) =>
        {
            AsyncDeserializeJsonCallback(callback, obj);
        }, timeout);

        return(task);
    }
Пример #30
0
    private static void AsyncDeserializeJsonCallback <T>(Action <bool, T> callback, Task <T> obj)
    {
        if (obj == null)
        {
#if UNITY_EDITOR
            VeerDebug.Log(" AsyncDeserializeJson failed by timeout ...");
#endif
            callback(false, default(T));
            return;
        }
        else if (obj.IsFaulted || obj.IsAborted || !obj.IsCompleted)
        {
            callback(false, default(T));
            return;
        }
        else
        {
            callback(true, obj.Result);
            return;
        }
    }