Ejemplo n.º 1
0
    public void TestJson()
    {
        //PerTest.RefObject refObj = new PerTest.RefObject();
        //refObj.x = Int32.MaxValue;
        //refObj.y = Int32.MinValue;
        //refObj.String = "hello";

        //string json = JsonMapper.ToJson(refObj);
        //Debug.LogError(json);
        //var jsonRefObj = JsonMapper.ToObject<PerTest.RefObject>(json);
        //Debug.LogError(jsonRefObj.x + "|" + jsonRefObj.y + "|" + jsonRefObj.String);
        //jsonRefObj.PrintLog();

        //EntityList entityList = new EntityList();
        List <Entity> entityList = new List <Entity>();
        Entity        entity     = new Entity();

        for (int i = 0; i < entity.map.Count; i++)
        {
            Debug.LogError(entity.map[i]);
        }
        entity.id   = 20160217;
        entity.name = "bozo";

        entity.coordinate = new Vector2(-1f, 3.14f);
        entity.pos        = new Vector3(-16.1f, 0.0f, 15.8f);
        Debug.LogError(entity.pos.GetHashCode());
        Debug.LogError(Vector3.MoveTowards(entity.pos, Vector3.zero, 2f));
        entity.v4         = new Vector4(-1f, 2.12f, 0f, 3f);
        entity.quaternion = new Quaternion(-21f, 31.48f, 77f, 1f);
        entity.color32    = Color.green;
        entity.color      = Color.cyan;
        entity.bounds     = new Bounds(entity.pos, Vector3.one * 3);
        entity.rect       = new Rect(0f, 0f, 800f, 600f);
        entity.rectOffset = new RectOffset(10, 20, 30, -40);

        entity.defaultAction = new EntityAction
        {
            id      = 6666,
            content = "Happy Day"
        };

        entity.simpleList.Add("abc");
        entity.simpleList.Add("efg");
        entity.simpleList.Add("ko");

        entity.simpleDic.Add("4001", "What's up");
        entity.simpleDic.Add("4002", "Good bye");
        var action = new EntityAction
        {
            id      = 1,
            content = "Hello World"
        };

        //Add方式添加
        entity.complexDic.Add(action.id.ToString(), action);
        entity.complexList.Add(action);

        //[]方式添加
        action = new EntityAction
        {
            id      = 2,
            content = "Help me"
        };
        entity.complexDic[action.id.ToString()] = action;
        entity.complexList.Add(action);

        //entityList.list.Add(entity);
        //entityList.list.Add(entity);
        entityList.Add(entity);
        entityList.Add(entity);
        string entityJson = JsHelper.ToJson(entity, true);

        Debug.LogError(entityJson);
        string listJson = JsHelper.ToJson(entityList);

        Debug.LogError(listJson);

        Debug.LogError("============Log jsonDic============");
        var    jsonDic = JsHelper.ToObject <Dictionary <string, object> >(JsHelper.ToJson(Vector3.down));
        string log     = "";

        foreach (var item in jsonDic)
        {
            log += "k:" + item.Key + " v:" + item.Value + "\n";
        }
        Debug.LogError(log);

        Debug.LogError("============Log jsonEntity============");
        Entity jsonEntity = JsHelper.ToObject <Entity>(entityJson);

        jsonEntity.PrintLog();
        Debug.LogError("============Log jsonEntityList============");
        List <Entity> jsonEntityList = JsHelper.ToCollection <List <Entity>, Entity>(listJson);

        foreach (var item in jsonEntityList)
        {
            PrintEntityInfo(item);
        }
    }