HasFields() public method

public HasFields ( string names ) : bool
names string
return bool
Beispiel #1
0
        /// <summary>
        /// Reconstructs the <see cref="CloudConfig"/> from a <see cref="JSONObject"/>.
        /// </summary>
        /// <param name="jsonObject"><see cref="JSONObject"/> containing the <see cref="CloudConfig"/>.</param>
        private void FromJSONObject(JSONObject jsonObject)
        {
            if (!jsonObject.HasFields(achievementIDsName, leaderboardIDsName, cloudVariablesName, appleSupportedName, googleSupportedName,
                                      amazonSupportedName, androidPlatformName, googleAppIDName, googleSetupRunName, debugModeEnabledName, versionName))
            {
                throw new SerializationException("JSONObject missing fields, cannot deserialize to " + typeof(CloudConfig).Name);
            }

            AchievementIDs   = EditorJsonHelper.Convert <List <PlatformIdData> >(jsonObject[achievementIDsName]);
            LeaderboardIDs   = EditorJsonHelper.Convert <List <PlatformIdData> >(jsonObject[leaderboardIDsName]);
            CloudVariables   = EditorJsonHelper.Convert <List <CloudVariableData> >(jsonObject[cloudVariablesName]);
            AppleSupported   = jsonObject[appleSupportedName].B;
            GoogleSupported  = jsonObject[googleSupportedName].B;
            AmazonSupported  = jsonObject[amazonSupportedName].B;
            AndroidPlatform  = (AndroidBuildPlatform)Enum.Parse(typeof(AndroidBuildPlatform), jsonObject[androidPlatformName].String);
            GoogleAppID      = jsonObject[googleAppIDName].String;
            GoogleSetupRun   = jsonObject[googleSetupRunName].B;
            DebugModeEnabled = jsonObject[debugModeEnabledName].B;
            Version          = jsonObject[versionName].String;
            if (jsonObject.HasFields(apiKeyName))
            {
                ApiKey = jsonObject[apiKeyName].String;
            }

            if (jsonObject.HasFields(settingsLocationName))
            {
                SettingsLocation = (SettingsLocation)Enum.Parse(typeof(SettingsLocation), jsonObject[settingsLocationName].String);
            }
        }
    public static bool IsPlayerJSONValid(JSONObject json)
    {
        bool ret = (json != null);

        if (ret)
        {
            ret = json.HasFields(FIELDS_REQUIRED);

            try
            {
                int.Parse(json.GetField(FIELD_SCORE).ToString());
            }
            catch (Exception)
            {
                ret = false;
            }

            if (ret && json.HasField(FIELD_ACHIEVEMENTS))
            {
                foreach (var kvp in json.GetField(FIELD_ACHIEVEMENTS).ToDictionary())
                {
                    try
                    {
                        Boolean.Parse(kvp.Value);
                    }
                    catch (Exception)
                    {
                        ret = false;
                    }
                }
            }
        }

        return(ret);
    }
Beispiel #3
0
        /// <summary>
        /// Reconstructs the <see cref="PlatformIdData"/> from a <see cref="JSONObject"/>.
        /// </summary>
        /// <param name="jsonObject"><see cref="JSONObject"/> containing the <see cref="PlatformIdData"/>.</param>
        private void FromJSONObject(JSONObject jsonObject)
        {
            if (!jsonObject.HasFields(internalIdName, appleIdName, googleIdName))
            {
                throw new SerializationException("JSONObject missing fields, cannot deserialize to " + typeof(PlatformIdData).Name);
            }

            InternalId = jsonObject[internalIdName].String;
            AppleId    = jsonObject[appleIdName].String;
            GoogleId   = jsonObject[googleIdName].String;
        }
Beispiel #4
0
 public static Vector3 ParseCoords(JSONObject Coordinates)
 {
     if ((Coordinates != null) && Coordinates.HasFields(new string[] { "x", "y", "z" }))
     {
         return(new Vector3(Coordinates["x"].f, Coordinates["y"].f, Coordinates["z"].f));
     }
     else
     {
         return(new Vector3());
     }
 }
Beispiel #5
0
        /// <summary>
        /// Reconstructs the <see cref="PlatformIdData"/> from a <see cref="JSONObject"/>.
        /// </summary>
        /// <param name="jsonObject"><see cref="JSONObject"/> containing the <see cref="PlatformIdData"/>.</param>
        private void FromJSONObject(JSONObject jsonObject)
        {
            if (!jsonObject.HasFields(c_keyInternalID, c_keyAppleID, c_keyGoogleID, c_keyAmazonID))
            {
                throw new SerializationException("JSONObject missing fields, cannot deserialize to " + typeof(PlatformIdData).Name);
            }

            InternalId = jsonObject[c_keyInternalID].String;
            AppleId    = jsonObject[c_keyAppleID].String;
            GoogleId   = jsonObject[c_keyGoogleID].String;
            AmazonId   = jsonObject[c_keyAmazonID].String;
        }
Beispiel #6
0
        /// <summary>
        /// Provides backwards compatibilty for old aliases. Used when a serialization alias has been changed.
        /// </summary>
        /// <param name="className">The name of the class that is being deserialized.</param>
        /// <param name="jsonObject">The <see cref="JSONObject"/> to check for the aliases.</param>
        /// <param name="aliases">The aliases to check for.</param>
        /// <returns>The alias used in the <see cref="JSONObject"/>.</returns>
        /// <exception cref="SerializationException">Thrown when none of the aliases provided exist in the <see cref="JSONObject"/>.</exception>
        public static string GetAlias(string className, JSONObject jsonObject, params string[] aliases)
        {
            foreach (var alias in aliases)
            {
                if (jsonObject.HasFields(alias))
                {
                    return(alias);
                }
            }

            throw new SerializationException("JSONObject missing fields, cannot deserialize to " + className);
        }
Beispiel #7
0
        /// <summary>
        /// Reconstructs the <see cref="CloudVariableData"/> from a <see cref="JSONObject"/>.
        /// </summary>
        /// <param name="jsonObject"><see cref="JSONObject"/> containing the <see cref="CloudVariableData"/>.</param>
        private void FromJSONObject(JSONObject jsonObject)
        {
            if (!jsonObject.HasFields(keyName, typeName, defaultValueName, persistenceTypeName, allowNegativeName))
            {
                throw new SerializationException("JSONObject missing fields, cannot deserialize to " + typeof(CloudVariableData).Name);
            }

            Key  = jsonObject[keyName].String;
            Type = (CloudVariableType)Enum.Parse(typeof(CloudVariableType), jsonObject[typeName].String);
            DefaultValueString = jsonObject[defaultValueName].String;
            PersistenceType    = (PersistenceType)Enum.Parse(typeof(PersistenceType), jsonObject[persistenceTypeName].String);
            AllowNegative      = jsonObject[allowNegativeName].B;
        }
        /// <summary>
        /// Reconstructs the <see cref="CloudVariableData"/> from a <see cref="JSONObject"/>.
        /// </summary>
        /// <param name="jsonObject"><see cref="JSONObject"/> containing the <see cref="CloudVariableData"/>.</param>
        private void FromJSONObject(JSONObject jsonObject)
        {
            if (!jsonObject.HasFields(c_keyKey, c_keyType, c_keyDefaultValue, c_keyPersistenceType, c_keyAllowNegative))
            {
                throw new SerializationException("JSONObject missing fields, cannot deserialize to " + typeof(CloudVariableData).Name);
            }

            Key  = jsonObject[c_keyKey].String;
            Type = (CloudVariableType)Enum.Parse(typeof(CloudVariableType), jsonObject[c_keyType].String);
            DefaultValueString = jsonObject[c_keyDefaultValue].String;
            PersistenceType    = (PersistenceType)Enum.Parse(typeof(PersistenceType), jsonObject[c_keyPersistenceType].String);
            AllowNegative      = jsonObject[c_keyAllowNegative].B;
        }
Beispiel #9
0
        /// <summary>
        /// Reconstructs the <see cref="CloudConfig"/> from a <see cref="JSONObject"/>.
        /// </summary>
        /// <param name="jsonObject"><see cref="JSONObject"/> containing the <see cref="CloudConfig"/>.</param>
        private void FromJSONObject(JSONObject jsonObject)
        {
            if (!jsonObject.HasFields(c_keyAchievementIDs, c_keyLeaderboardIDs, c_keyCloudVariables, c_keyAppleSupported, c_keyGoogleSupported,
                                      c_keyAmazonSupported, c_keyAndroidPlatform, c_keyGoogleAppID, c_keyGoogleSetupRun, c_keyDebugModeEnabled, c_keyVersion))
            {
                throw new SerializationException("JSONObject missing fields, cannot deserialize to " + typeof(CloudConfig).Name);
            }

            AchievementIDs   = EditorJsonHelper.Convert <List <PlatformIdData> >(jsonObject[c_keyAchievementIDs]);
            LeaderboardIDs   = EditorJsonHelper.Convert <List <PlatformIdData> >(jsonObject[c_keyLeaderboardIDs]);
            CloudVariables   = EditorJsonHelper.Convert <List <CloudVariableData> >(jsonObject[c_keyCloudVariables]);
            AppleSupported   = jsonObject[c_keyAppleSupported].B;
            GoogleSupported  = jsonObject[c_keyGoogleSupported].B;
            AmazonSupported  = jsonObject[c_keyAmazonSupported].B;
            AndroidPlatform  = (AndroidBuildPlatform)Enum.Parse(typeof(AndroidBuildPlatform), jsonObject[c_keyAndroidPlatform].String);
            GoogleAppID      = jsonObject[c_keyGoogleAppID].String;
            GoogleSetupRun   = jsonObject[c_keyGoogleSetupRun].B;
            DebugModeEnabled = jsonObject[c_keyDebugModeEnabled].B;
            Version          = jsonObject[c_keyVersion].String;
            if (jsonObject.HasFields(c_apiKey))
            {
                ApiKey = jsonObject[c_apiKey].String;
            }
        }
    private void Run()
    {
        string fileData = File.ReadAllText (json_location);
        JSONObject data = new JSONObject (fileData);

        Uri assetsPath = new Uri (Application.dataPath);
        Uri jsonPath = new Uri (System.IO.Path.GetDirectoryName (json_location));
        Uri diff = assetsPath.MakeRelativeUri (jsonPath);
        string wd = diff.OriginalString+System.IO.Path.DirectorySeparatorChar;

        //test data
        if(!data.HasFields(new string[]{"framerate","images","frames","animations"}))
        {
            Debug.LogError("Error: json file must contain framerate, images, frames, and animations.");
            return;
        }

        //generate sprite frames
        List<JSONObject> frames_data = data.GetField ("frames").list;
        List<JSONObject> images_data = data.GetField ("images").list;

        // load textures
        List<Texture2D> images = new List<Texture2D>();
        List<List<SpriteMetaData>> sprite_metadata = new List<List<SpriteMetaData>> ();
        for(int i=0; i<images_data.Count; i++)
        {
            string path = wd+images_data[i].str;
            Texture2D tex = AssetDatabase.LoadMainAssetAtPath(path) as Texture2D;
            images.Add(tex);
            sprite_metadata.Add (new List<SpriteMetaData> ());
        }

        //set meta data based on frames
        for(int i=0; i<frames_data.Count; i++)
        {
            List<JSONObject> frame = frames_data[i].list;
            float x = frame[0].f;
            float y = frame[1].f;
            float w = frame[2].f;
            float h = frame[3].f;
            int img = (int)frame[4].f;
            float rx = frame[5].f;
            float ry = frame[6].f;

            float imgHeight = (float)images[img].height;

            SpriteMetaData meta = new SpriteMetaData{
                alignment = (int)SpriteAlignment.Custom,
                border = new Vector4(),
                name = prefix+"_"+(i).ToString(),
                pivot = new Vector2(rx/w,1-(ry/h)),
                rect = new Rect(x, imgHeight - y - h, w, h)
            };
            sprite_metadata[img].Add(meta);
        }

        //save data back
        for(int i=0; i<images.Count; i++)
        {
            TextureImporter importer = TextureImporter.GetAtPath(wd+images_data[i].str) as TextureImporter;
            //importer.assetPath = images_data[i].str;
            importer.mipmapEnabled = false;
            importer.textureType = TextureImporterType.Sprite;
            importer.spriteImportMode = SpriteImportMode.Multiple;
            importer.spritesheet = sprite_metadata[i].ToArray();

            try
            {
                AssetDatabase.StartAssetEditing();
                AssetDatabase.ImportAsset(importer.assetPath);
            }
            finally
            {
                AssetDatabase.StopAssetEditing();
            }
        }

        //load sprite dictionary
        Dictionary<String,Sprite> sprites = new Dictionary<string, Sprite> ();
        for(int i=0; i<images_data.Count; i++)
        {
            Sprite[] sp = AssetDatabase.LoadAllAssetsAtPath( wd+images_data[i].str ).OfType<Sprite>().ToArray();
            for(int j=0; j<sp.Length; j++)
            {
                sprites[sp[j].name] = sp[j];
            }
        }

        //create animations
        int fps = (int)data.GetField ("framerate").f;
        List<string> animation_names = data.GetField ("animations").keys;

        foreach(string animationName in animation_names)
        {
            JSONObject animationJson = data.GetField("animations").GetField(animationName);
            List<JSONObject> frame_Data = animationJson.GetField("frames").list;
            float fpsinc = 1/(float)fps;

            EditorCurveBinding curveBinding = new EditorCurveBinding();
            curveBinding.type = typeof(SpriteRenderer);
            curveBinding.path = "";
            curveBinding.propertyName = "m_Sprite";

            List<ObjectReferenceKeyframe> keyframes = new List<ObjectReferenceKeyframe>();
            string lastFrame = "";
            for(int i=0; i<frame_Data.Count; i++)
            {
                string fname = frame_Data[i].f.ToString();
                if(i == frame_Data.Count-1 || fname!=lastFrame)
                {
                    ObjectReferenceKeyframe k = new ObjectReferenceKeyframe();
                    k.time = i*fpsinc;
                    k.value = sprites[prefix+"_"+fname];
                    keyframes.Add(k);
                    lastFrame = fname;
                }
            }

            AnimationClip clip = new AnimationClip();
            clip.frameRate = (float)fps;
            clip.legacy = false;
            clip.EnsureQuaternionContinuity();

            AnimationUtility.SetObjectReferenceCurve(clip, curveBinding, keyframes.ToArray());

            //load asset if it exists, else create new
            AnimationClip a = AssetDatabase.LoadAssetAtPath<AnimationClip>(wd + animationName + ".anim");
            if(a!= null)
            {
                Debug.Log("update clip");
                clip.wrapMode = a.wrapMode;
                a = clip;
            } else
            {
                AssetDatabase.CreateAsset(clip, wd + animationName + ".anim");
            }
        }
        AssetDatabase.SaveAssets();
    }