Commit 1fd7153d authored by Ever's avatar Ever

解析JSON赋值

parent c38a2830
......@@ -34,8 +34,12 @@ public class GameEditor
}
else if (path.EndsWith(".json"))
{
//AssetDatabase.LoadAssetAtPath<JsonData>(path);
//md.json = JsonMapper.ToObject(webRequest.downloadHandler.text);
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
string str = sr.ReadToEnd();
md.json = JsonUtility.FromJson<ResJsonData>(str);
sr.Close();
fs.Close();
}
}
md.Init();
......
fileFormatVersion: 2
guid: 3a461ffb57d2c3d4799b655b39f57c5c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -39,10 +39,10 @@ public class Drive : MonoBehaviour
poolList.Add(temp);
}
}
public void Create(MainData md, ImageSubData isd, Vector3 v)
public void Create(MainData md, RoomDatas rd, Vector3 v)
{
ExMask temp = Get();
temp.DoFun(md, isd, v);
temp.DoFun(md, rd, v);
}
}
......@@ -33,19 +33,19 @@ public class ExMask : MonoBehaviour
}
}
internal void DoFun(MainData md, ImageSubData isd, Vector3 v)
internal void DoFun(MainData md, RoomDatas rd, Vector3 v)
{
//创建图片
Texture2D te = new Texture2D((int)(isd.size.z - isd.size.x + 1), (int)(isd.size.w - isd.size.y + 1), TextureFormat.RGBA32, false);
Texture2D te = new Texture2D((int)(rd.size.z - rd.size.x + 1), (int)(rd.size.w - rd.size.y + 1), TextureFormat.RGBA32, false);
Color[] colors = te.GetPixels();
for (int i = 0; i < colors.Length; i++)
{
colors[i].a = 0f;
}
te.SetPixels(colors);
for (int i = 0; i < isd.list.Count; i++)
for (int i = 0; i < rd.list.Count; i++)
{
te.SetPixel((int)(isd.list[i].x - isd.size.x), (int)(isd.list[i].y - isd.size.y), Color.white);
te.SetPixel((int)(rd.list[i].x - rd.size.x), (int)(rd.list[i].y - rd.size.y), Color.white);
}
te.Apply();
Sprite sp = Sprite.Create(te, new Rect(0, 0, te.width, te.height), Vector2.zero);
......@@ -59,14 +59,14 @@ public class ExMask : MonoBehaviour
Rect.SetParent(p);
SPRect.SetParent(md.png.transform);
SPRect.localPosition = new Vector3(isd.size.x + (isd.size.z + 1 - isd.size.x) / 2 - (md.Te_png.width + 1) / 2, isd.size.y + (isd.size.w + 1 - isd.size.y) / 2 - (md.Te_png.height + 1) / 2, 0);
SPRect.localPosition = new Vector3(rd.size.x + (rd.size.z + 1 - rd.size.x) / 2 - (md.Te_png.width + 1) / 2, rd.size.y + (rd.size.w + 1 - rd.size.y) / 2 - (md.Te_png.height + 1) / 2, 0);
SPRect.SetParent(Rect);
//初始处理
gameObject.SetActive(true);
Rect.sizeDelta = Vector2.zero;
float a = Math.Max(isd.size.z - v.x, v.x - isd.size.x);
float b = Math.Max(isd.size.w - v.y, v.y - isd.size.y);
float a = Math.Max(rd.size.z - v.x, v.x - rd.size.x);
float b = Math.Max(rd.size.w - v.y, v.y - rd.size.y);
IsUse = Math.Max(a, b) * 2;
}
}
......@@ -14,9 +14,12 @@ public class ImageData
public List<ImageSubData> AlllList = new List<ImageSubData>();
public void AddData(ImageSubData isd)
{
if (isd.list.Count > 1)
{
AlllList.Add(isd);
}
}
}
[Serializable]
......
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
......@@ -8,6 +9,7 @@ using UnityEngine.UI;
public class MainData : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
{
public static int GunSize = 50;
public ImageData data = new ImageData();
public Texture2D Te_png;
......@@ -64,34 +66,31 @@ public class MainData : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDrag
#region 图片分区
public void Split()
{
for (int i = 0; i < Te_png.width; i++)
{
for (int j = 0; j < Te_png.height; j++)
for (int i = 0; i < json.roomDatas.Count; i++)
{
Vector2 v = new Vector2(i, j);
RoomDatas temp = json.roomDatas[i];
Vector2 v = new Vector2(temp.px, temp.py);
if (Te_png.GetPixel((int)v.x, (int)v.y).a <= 0f)
{
FunDo(v);
}
FunDo(v, temp);
}
}
Te_png.Apply();
}
private Stack<Vector2> stackVector2 = new Stack<Vector2>();
public void FunDo(Vector2 _v)
public void FunDo(Vector2 _v, RoomDatas temp)
{
stackVector2.Push(_v);
Vector2[] d = new Vector2[4] { new Vector2(0, -1), new Vector2(0, 1), new Vector2(-1, 0), new Vector2(1, 0) };
Vector2 v_1;
Vector2 v_2;
ImageSubData temp = new ImageSubData();
while (stackVector2.Count > 0)
{
v_1 = stackVector2.Pop();
temp.AddData(v_1);
Te_png.SetPixel((int)v_1.x, (int)v_1.y, Color.white);
for (int i = 0; i < 4; i++)
for (int i = 0; i < d.Length; i++)
{
v_2 = v_1 + d[i];
if (v_2.x >= 0 && v_2.x < Te_png.width && v_2.y >= 0 && v_2.y < Te_png.height && Te_png.GetPixel((int)v_2.x, (int)v_2.y).a <= 0f)
......@@ -100,7 +99,6 @@ public class MainData : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDrag
}
}
}
data.AddData(temp);
}
#endregion
......@@ -241,27 +239,27 @@ public class MainData : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDrag
//Debug.Log("涂过了");
return false;
}
ImageSubData isd = null;
RoomDatas rd = null;
for (int i = 0; i < data.AlllList.Count; i++)
{
if (data.AlllList[i].Contains(v))
if (json.roomDatas[i].Contains(v))
{
//Debug.Log("在第" + i + "组数据中");
isd = data.AlllList[i];
rd = json.roomDatas[i];
}
}
if (isd == null)
if (rd == null)
{
//Debug.Log("组数据为空");
return false;
}
Drive.Ins.Create(this, isd, v);
Drive.Ins.Create(this, rd, v);
for (int i = 0; i < isd.list.Count; i++)
for (int i = 0; i < rd.list.Count; i++)
{
Te_png.SetPixel((int)isd.list[i].x, (int)isd.list[i].y, Color.clear);
Te_png.SetPixel((int)rd.list[i].x, (int)rd.list[i].y, Color.clear);
}
Te_png.Apply();
return true;
......
......@@ -3,13 +3,17 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
[Serializable]
public class ResJsonData
{
public List<int> list = new List<int>();
public List<RoomDatas> roomDatas = new List<RoomDatas>();
public List<RoomGroupsData> roomGroupsData = new List<RoomGroupsData>();
public int colorDiff;
public int lineFactorDiff;
public int size;
......@@ -27,6 +31,7 @@ public class ResJsonData
public int scaleNumWhenOver100;
public ExtraMetaData extraMetaData;
}
[Serializable]
public class ExtraMetaData //后面再看为啥json序列化失败
{
......@@ -34,6 +39,7 @@ public class ExtraMetaData //后面再看为啥json序列化失败
public int tuhei_size;
}
[Serializable]
public class RoomDatas
{
public int id;
......@@ -46,7 +52,35 @@ public class RoomDatas
public int G;
public int B;
public int useAreaData;
public Vector4 size = -Vector4.one;
public List<Vector2> list = new List<Vector2>();
public void AddData(Vector2 v)
{
if (size.x == -1 || size.x > v.x)
{
size.x = v.x;
}
if (size.y == -1 || size.y > v.y)
{
size.y = v.y;
}
if (size.z == -1 || size.z < v.x)
{
size.z = v.x;
}
if (size.w == -1 || size.w < v.y)
{
size.w = v.y;
}
list.Add(v);
}
public bool Contains(Vector2 v)
{
return list.Contains(v);
}
}
[Serializable]
public class RoomGroupsData
{
public int id;
......
fileFormatVersion: 2
guid: da5799bf52418e94fa8e8da3423f5d00
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment