本文整理汇总了C#中JSONObject.HasFields方法的典型用法代码示例。如果您正苦于以下问题:C# JSONObject.HasFields方法的具体用法?C# JSONObject.HasFields怎么用?C# JSONObject.HasFields使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JSONObject
的用法示例。
在下文中一共展示了JSONObject.HasFields方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
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);
//.........这里部分代码省略.........