示例#1
0
    public static string serialize(GameObject obj)
    {
        string serialized = "";                        //Message.GO_UPDATE.ToString();

        serMarkerFlag serMarker = new serMarkerFlag(); //Put the marker flag info on the string first !!!

        serMarker.flag = obj.GetComponent <MarkerFlag>();

        if (obj.transform.parent == null)
        {
            serMarker.flag.parentID = "__";
        }
        else
        {
            serMarker.flag.parentID = obj.transform.parent.GetComponent <MarkerFlag>().id;
        }

        string flagData = new string(serMarker.toChar());

        serialized += flagData;

        int hashLoc = genHashCode(serMarker.flag.id);
        int xLoc    = hashLoc % 10;
        int yLoc    = hashLoc % 100;

        //TODO check location if it already is there
        objectMap[xLoc, yLoc].Add(serMarker.flag);

        serialized += obj.name + "|";
        serialized += obj.tag + "|";
        serialized += obj.layer + "|";
        serialized += obj.isStatic + "|";
        Component[] comps;
        comps = obj.GetComponents <Component>();
        for (int i = 0; i < comps.Length; i++)
        {
            if (comps[i] != null)
            {
                if (comps[i].GetType() == typeof(UnityEngine.Transform))
                {
                    UnityEngine.Transform newTransform = comps[i] as UnityEngine.Transform;

                    Transform serNewTransform = new Transform();
                    serNewTransform.pos   = newTransform.position;
                    serNewTransform.rot   = newTransform.rotation;
                    serNewTransform.scale = newTransform.localScale;
                    string transformString = new string(serNewTransform.toChar());
                    serialized += transformString;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.BoxCollider))
                {
                    UnityEngine.BoxCollider newBoxCollider = comps[i] as UnityEngine.BoxCollider;

                    BoxCollider serNewBoxCollider = new BoxCollider();
                    serNewBoxCollider.center    = newBoxCollider.center;
                    serNewBoxCollider.size      = newBoxCollider.size;
                    serNewBoxCollider.isTrigger = newBoxCollider.isTrigger;
                    string boxColliderString = new string(serNewBoxCollider.toChar());
                    serialized += boxColliderString;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.SphereCollider))
                {
                    UnityEngine.SphereCollider newSphereCollider = comps[i] as UnityEngine.SphereCollider;

                    SphereCollider serNewSphereCollider = new SphereCollider();
                    serNewSphereCollider.center    = newSphereCollider.center;
                    serNewSphereCollider.radius    = newSphereCollider.radius;
                    serNewSphereCollider.isTrigger = newSphereCollider.isTrigger;
                    string sphereColliderString = new string(serNewSphereCollider.toChar());
                    serialized += sphereColliderString;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.CapsuleCollider))
                {
                    UnityEngine.CapsuleCollider newCapsuleCollider = comps[i] as UnityEngine.CapsuleCollider;

                    CapsuleCollider serNewCapsuleCollider = new CapsuleCollider();
                    serNewCapsuleCollider.center        = newCapsuleCollider.center;
                    serNewCapsuleCollider.radius        = newCapsuleCollider.radius;
                    serNewCapsuleCollider.height        = newCapsuleCollider.height;
                    serNewCapsuleCollider.directionAxis = newCapsuleCollider.direction;
                    serNewCapsuleCollider.isTrigger     = newCapsuleCollider.isTrigger;
                    string capsuleColliderString = new string(serNewCapsuleCollider.toChar());
                    serialized += capsuleColliderString;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.Rigidbody))
                {
                    UnityEngine.Rigidbody newRigidBody = comps[i] as UnityEngine.Rigidbody;

                    RigidBody serNewRigidBody = new RigidBody();
                    serNewRigidBody.mass               = newRigidBody.mass;
                    serNewRigidBody.drag               = newRigidBody.drag;
                    serNewRigidBody.angularDrag        = newRigidBody.angularDrag;
                    serNewRigidBody.interpolate        = (int)newRigidBody.interpolation;
                    serNewRigidBody.collisionDetection = newRigidBody.detectCollisions;
                    serNewRigidBody.freeze             = (int)newRigidBody.constraints;
                    serNewRigidBody.isKinematic        = newRigidBody.isKinematic;
                    serNewRigidBody.useGravity         = newRigidBody.useGravity;
                    serNewRigidBody.collisionDetection = newRigidBody.detectCollisions;
                    string rigidBodyString = new string(serNewRigidBody.toChar());
                    serialized += rigidBodyString;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.Camera))
                {
                    UnityEngine.Camera newCamera = comps[i] as UnityEngine.Camera;

                    Camera serNewCamera = new Camera();
                    serNewCamera.clearFlags       = (int)newCamera.clearFlags;
                    serNewCamera.background       = newCamera.backgroundColor;
                    serNewCamera.cullingMask      = newCamera.cullingMask;
                    serNewCamera.projection       = newCamera.projectionMatrix.ToString();
                    serNewCamera.near             = newCamera.nearClipPlane;
                    serNewCamera.far              = newCamera.farClipPlane;
                    serNewCamera.viewportRect     = newCamera.rect;
                    serNewCamera.renderingPath    = (int)newCamera.renderingPath;
                    serNewCamera.HDR              = newCamera.allowHDR;
                    serNewCamera.MSAA             = newCamera.allowMSAA;
                    serNewCamera.occlusionCulling = newCamera.useOcclusionCulling;
                    serNewCamera.depth            = newCamera.depth;
                    serNewCamera.fov              = newCamera.fieldOfView;
                    serNewCamera.targetDisplay    = newCamera.targetDisplay;

                    string cameraString = new string(serNewCamera.toChar());
                    serialized += cameraString;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.MeshFilter))
                {
                    //Gather Meshfilter information on current GO
                    UnityEngine.MeshFilter gOMeshFilter = comps[i] as UnityEngine.MeshFilter;
                    Mesh gOMesh = gOMeshFilter.sharedMesh;


                    //Pack data into our meshfilter object
                    MeshFilter meshStruct = new MeshFilter();
                    meshStruct.filePath = AssetDatabase.GetAssetPath(gOMesh);
                    meshStruct.meshName = gOMesh.name;

                    //Convert the data into a string and add it to the overall data stream
                    string sStream = new string(meshStruct.toChar());
                    serialized += sStream;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.MeshRenderer))
                {
                    UnityEngine.MeshRenderer gOMeshRenderer = comps[i] as UnityEngine.MeshRenderer;


                    //Pack data into our MeshRenderer obj
                    MeshRenderer meshStruct = new MeshRenderer();
                    meshStruct.lightProbe      = (int)gOMeshRenderer.lightProbeUsage;
                    meshStruct.reflectionProbe = (int)gOMeshRenderer.reflectionProbeUsage;
                    meshStruct.castShadows     = (int)gOMeshRenderer.shadowCastingMode;
                    meshStruct.receiveShadows  = gOMeshRenderer.receiveShadows;
                    meshStruct.motionVectors   = (int)gOMeshRenderer.motionVectorGenerationMode;
                    meshStruct.lightmapStatic  = false;

                    Material[] gOMaterials = gOMeshRenderer.sharedMaterials;
                    for (int q = 0; q < gOMaterials.Length; ++q)
                    {
                        string materialPath = "";
                        if (gOMaterials[q] == null || gOMaterials[q].name == "Default-Material")
                        {
                            materialPath = "Default-Material";
                        }
                        else
                        {
                            materialPath = AssetDatabase.GetAssetPath(gOMaterials[q]);
                        }
                        meshStruct.materialFiles.Add(materialPath);
                    }

                    string sStream = new string(meshStruct.toChar());
                    serialized += sStream;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.Light))
                {
                    UnityEngine.Light newLight = comps[i] as UnityEngine.Light;

                    Light serNewLight = new Light();
                    serNewLight.type        = (int)newLight.type;
                    serNewLight.shadows     = (int)newLight.shadows;
                    serNewLight.mode        = (int)newLight.renderMode;
                    serNewLight.cullingMask = newLight.cullingMask;
                    serNewLight.color       = newLight.color;
                    serNewLight.intensity   = newLight.intensity;
                    serNewLight.cookie      = newLight.cookieSize;

                    string lightString = new string(serNewLight.toChar());
                    serialized += lightString;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.GUILayer))
                {
                    UnityEngine.GUILayer newGuiLayer = comps[i] as UnityEngine.GUILayer;

                    string lightString = "guilayer|";
                    serialized += lightString;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.FlareLayer))
                {
                    UnityEngine.FlareLayer newGuiLayer = comps[i] as UnityEngine.FlareLayer;

                    string lightString = "flarelayer|";
                    serialized += lightString;
                }
                else if (comps[i].GetType() == typeof(UnityEngine.AudioListener))
                {
                    UnityEngine.AudioListener newGuiLayer = comps[i] as UnityEngine.AudioListener;

                    string lightString = "audiolistener|";
                    serialized += lightString;
                }
            }
        }

        return(serialized);
    }