376 lines
8.1 KiB
C#
376 lines
8.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BitStrap;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ObjectJsonSerializer : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public class MyGameObject
|
|
{
|
|
[NonSerialized]
|
|
public Transform source;
|
|
|
|
public string name;
|
|
|
|
public List<MyGameObject> children;
|
|
|
|
public ButtonMyComponent cButtonMyComponent;
|
|
|
|
public TextMyComponent cTextMyComponent;
|
|
|
|
public ImageMyComponent cImageMyComponent;
|
|
|
|
public TransformMyComponent cTransform;
|
|
|
|
public AquariumControllerMyComponent cAquariumController;
|
|
|
|
public BoxColliderMyComponent cBoxCollider;
|
|
|
|
public LightMyComponent cLight;
|
|
|
|
public MeshRendererMyComponent cMeshRenderer;
|
|
|
|
public MeshFilterMyComponent cMeshFilter;
|
|
|
|
public MeshColliderMyComponent cMeshCollider;
|
|
|
|
public LODGroupMyComponent cLODGroup;
|
|
|
|
public SphereColliderMyComponent cSphereCollider;
|
|
|
|
public CapsuleColliderMyComponent cCapsuleCollider;
|
|
|
|
public MyGameObject(Transform source)
|
|
{
|
|
this.source = source;
|
|
name = source.name;
|
|
children = new List<MyGameObject>();
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class MyComponent
|
|
{
|
|
public bool created;
|
|
|
|
public MyComponent()
|
|
{
|
|
created = true;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class _MyComponent : MyComponent
|
|
{
|
|
public _MyComponent(Component source)
|
|
{
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class ButtonMyComponent : MyComponent
|
|
{
|
|
public Selectable.Transition transition;
|
|
|
|
public ColorBlock colors;
|
|
|
|
public SpriteState spriteState;
|
|
|
|
public AnimationTriggers animationTriggers;
|
|
|
|
public Graphic targetGraphic;
|
|
|
|
public ButtonMyComponent(Button source)
|
|
{
|
|
transition = source.transition;
|
|
colors = source.colors;
|
|
spriteState = source.spriteState;
|
|
animationTriggers = source.animationTriggers;
|
|
targetGraphic = source.targetGraphic;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class TextMyComponent : MyComponent
|
|
{
|
|
public string text;
|
|
|
|
public Font font;
|
|
|
|
public int fontSize;
|
|
|
|
public Color color;
|
|
|
|
public TextMyComponent(Text source)
|
|
{
|
|
text = source.text;
|
|
font = source.font;
|
|
fontSize = source.fontSize;
|
|
color = source.color;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class ImageMyComponent : MyComponent
|
|
{
|
|
public Sprite sprite;
|
|
|
|
public Color color;
|
|
|
|
public ImageMyComponent(Image source)
|
|
{
|
|
sprite = source.sprite;
|
|
color = source.color;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class TransformMyComponent : MyComponent
|
|
{
|
|
public Vector3 localPosition;
|
|
|
|
public Quaternion localRotation;
|
|
|
|
public Vector3 localScale;
|
|
|
|
public TransformMyComponent(Transform source)
|
|
{
|
|
localPosition = source.localPosition;
|
|
localRotation = source.localRotation;
|
|
localScale = source.localScale;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class AquariumControllerMyComponent : MyComponent
|
|
{
|
|
public AquariumControllerMyComponent(AquariumController source)
|
|
{
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class BoxColliderMyComponent : MyComponent
|
|
{
|
|
public BoxColliderMyComponent(BoxCollider source)
|
|
{
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class LightMyComponent : MyComponent
|
|
{
|
|
public LightMyComponent(Light source)
|
|
{
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class MeshRendererMyComponent : MyComponent
|
|
{
|
|
public MeshRendererMyComponent(MeshRenderer source)
|
|
{
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class MeshFilterMyComponent : MyComponent
|
|
{
|
|
public MeshFilterMyComponent(MeshFilter source)
|
|
{
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class LODGroupMyComponent : MyComponent
|
|
{
|
|
public LODGroupMyComponent(LODGroup source)
|
|
{
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class MeshColliderMyComponent : MyComponent
|
|
{
|
|
public MeshColliderMyComponent(MeshCollider source)
|
|
{
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class SphereColliderMyComponent : MyComponent
|
|
{
|
|
public SphereColliderMyComponent(SphereCollider source)
|
|
{
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class CapsuleColliderMyComponent : MyComponent
|
|
{
|
|
public CapsuleColliderMyComponent(CapsuleCollider source)
|
|
{
|
|
}
|
|
}
|
|
|
|
public MyGameObject serializedObject;
|
|
|
|
[Button]
|
|
public void Serialize()
|
|
{
|
|
serializedObject = new MyGameObject(base.transform);
|
|
SerializeTarget(serializedObject);
|
|
}
|
|
|
|
public void SerializeTarget(MyGameObject target)
|
|
{
|
|
SerializeComponents(target);
|
|
foreach (Transform item in target.source)
|
|
{
|
|
MyGameObject myGameObject = new MyGameObject(item);
|
|
target.children.Add(myGameObject);
|
|
SerializeTarget(myGameObject);
|
|
}
|
|
}
|
|
|
|
[Button]
|
|
public void Deserialize()
|
|
{
|
|
if (serializedObject != null)
|
|
{
|
|
DeserializeTarget(serializedObject);
|
|
}
|
|
}
|
|
|
|
public void DeserializeTarget(MyGameObject target)
|
|
{
|
|
DeserializeComponents(target);
|
|
foreach (Transform item in target.source)
|
|
{
|
|
MyGameObject target2 = new MyGameObject(item);
|
|
DeserializeTarget(target2);
|
|
}
|
|
}
|
|
|
|
public void DeserializeComponents(MyGameObject target)
|
|
{
|
|
if (target.cButtonMyComponent != null)
|
|
{
|
|
Button button = target.source.gameObject.GetComponent<Button>();
|
|
if (button == null)
|
|
{
|
|
button = target.source.gameObject.AddComponent<Button>();
|
|
}
|
|
DeserializeButton(target.cButtonMyComponent, button);
|
|
}
|
|
if (target.cTextMyComponent != null)
|
|
{
|
|
Text text = target.source.gameObject.GetComponent<Text>();
|
|
if (text == null)
|
|
{
|
|
text = target.source.gameObject.AddComponent<Text>();
|
|
}
|
|
DeserializeText(target.cTextMyComponent, text);
|
|
}
|
|
if (target.cImageMyComponent != null)
|
|
{
|
|
Image image = target.source.gameObject.GetComponent<Image>();
|
|
if (image == null)
|
|
{
|
|
image = target.source.gameObject.AddComponent<Image>();
|
|
}
|
|
DeserializeImage(target.cImageMyComponent, image);
|
|
}
|
|
}
|
|
|
|
private void SerializeComponents(MyGameObject target)
|
|
{
|
|
Component[] components = target.source.GetComponents<Component>();
|
|
Component[] array = components;
|
|
foreach (Component component in array)
|
|
{
|
|
if (component is Transform)
|
|
{
|
|
target.cTransform = new TransformMyComponent(component as Transform);
|
|
}
|
|
else if (component is Button)
|
|
{
|
|
target.cButtonMyComponent = new ButtonMyComponent(component as Button);
|
|
}
|
|
else if (component is Text)
|
|
{
|
|
target.cTextMyComponent = new TextMyComponent(component as Text);
|
|
}
|
|
else if (component is Image)
|
|
{
|
|
target.cImageMyComponent = new ImageMyComponent(component as Image);
|
|
}
|
|
else if (component is AquariumController)
|
|
{
|
|
target.cAquariumController = new AquariumControllerMyComponent(component as AquariumController);
|
|
}
|
|
else if (component is BoxCollider)
|
|
{
|
|
target.cBoxCollider = new BoxColliderMyComponent(component as BoxCollider);
|
|
}
|
|
else if (component is Light)
|
|
{
|
|
target.cLight = new LightMyComponent(component as Light);
|
|
}
|
|
else if (component is MeshRenderer)
|
|
{
|
|
target.cMeshRenderer = new MeshRendererMyComponent(component as MeshRenderer);
|
|
}
|
|
else if (component is MeshFilter)
|
|
{
|
|
target.cMeshFilter = new MeshFilterMyComponent(component as MeshFilter);
|
|
}
|
|
else if (component is MeshCollider)
|
|
{
|
|
target.cMeshCollider = new MeshColliderMyComponent(component as MeshCollider);
|
|
}
|
|
else if (component is LODGroup)
|
|
{
|
|
target.cLODGroup = new LODGroupMyComponent(component as LODGroup);
|
|
}
|
|
else if (component is SphereCollider)
|
|
{
|
|
target.cSphereCollider = new SphereColliderMyComponent(component as SphereCollider);
|
|
}
|
|
else if (component is CapsuleCollider)
|
|
{
|
|
target.cCapsuleCollider = new CapsuleColliderMyComponent(component as CapsuleCollider);
|
|
}
|
|
else if (!(component is Animator) && !(component is RefreshIceFishing) && !(component is ChangeLayerAtPosition))
|
|
{
|
|
Debug.LogError("Component not defined: " + component, component);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void DeserializeButton(ButtonMyComponent buttonData, Button button)
|
|
{
|
|
button.transition = buttonData.transition;
|
|
button.colors = buttonData.colors;
|
|
button.spriteState = buttonData.spriteState;
|
|
button.animationTriggers = buttonData.animationTriggers;
|
|
button.targetGraphic = buttonData.targetGraphic;
|
|
}
|
|
|
|
public static void DeserializeText(TextMyComponent textData, Text text)
|
|
{
|
|
text.text = textData.text;
|
|
text.font = textData.font;
|
|
text.fontSize = textData.fontSize;
|
|
text.color = textData.color;
|
|
}
|
|
|
|
public static void DeserializeImage(ImageMyComponent imageData, Image image)
|
|
{
|
|
image.sprite = imageData.sprite;
|
|
image.color = imageData.color;
|
|
}
|
|
}
|