Exemplo n.º 1
0
    void OnEnable()
    {
        Buoyancy boyancy = target as Buoyancy;

        presetPath = Application.dataPath + "/Ocean/Editor/_OceanPresets";

        rrig = boyancy.GetComponent <Rigidbody>();
        if (rrig == null)
        {
            Debug.Log("Object requires a Rigidbody");
        }

        presetPath = Application.dataPath + "/Ocean/Editor/_OceanPresets";

        col = boyancy.GetComponent <BoxCollider>();
        if (col == null)
        {
            Debug.Log("Object requires a Box Collider");
        }
    }
Exemplo n.º 2
0
    void OnEnable()
    {
        Buoyancy boyancy = target as Buoyancy;

        var script = MonoScript.FromScriptableObject(this);

        presetPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(script)) + "/_OceanPresets";

        rrig = boyancy.GetComponent <Rigidbody>();
        if (rrig == null)
        {
            Debug.Log("Object requires a Rigidbody");
        }

        presetPath = Application.dataPath + "/Ocean/Editor/_OceanPresets";

        col = boyancy.GetComponent <BoxCollider>();
        if (col == null)
        {
            Debug.Log("Object requires a Box Collider");
        }
    }
Exemplo n.º 3
0
    //loads a buoyancy preset (and boat controller settings if available)
    public static bool loadPreset(Buoyancy buoyancy)
    {
        string preset = EditorUtility.OpenFilePanel("Load Ocean preset", presetPath, "buoyancy");

        if (!Application.isPlaying)
        {
            if (preset != null)
            {
                if (preset.Length > 0)
                {
                    if (File.Exists(preset))
                    {
                        using (BinaryReader br = new BinaryReader(File.Open(preset, FileMode.Open))){
                            bool hasrigidbody = false, hascollider = false;

                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                hasrigidbody = br.ReadBoolean();
                            }

                            if (hasrigidbody)
                            {
                                float mass = 1, drag = 1, andrag = 1;
                                bool  usegrav = true;
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    mass = br.ReadSingle();
                                }
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    drag = br.ReadSingle();
                                }
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    andrag = br.ReadSingle();
                                }
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    usegrav = br.ReadBoolean();
                                }
                                if (rrig != null)
                                {
                                    rrig.mass = mass; rrig.drag = drag; rrig.angularDrag = andrag; rrig.useGravity = usegrav;
                                }
                                else
                                {
                                    Debug.Log("No rigid body found");
                                }
                            }

                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                hascollider = br.ReadBoolean();
                            }

                            if (hascollider)
                            {
                                float x = 0, y = 0, z = 0, sx = 1, sy = 1, sz = 1;
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    x = br.ReadSingle();
                                }
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    y = br.ReadSingle();
                                }
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    z = br.ReadSingle();
                                }
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    sx = br.ReadSingle();
                                }
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    sy = br.ReadSingle();
                                }
                                if (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    sz = br.ReadSingle();
                                }

                                if (col != null)
                                {
                                    col.center = new Vector3(x, y, z);
                                    col.size   = new Vector3(sx / buoyancy.transform.localScale.x, sy / buoyancy.transform.localScale.y, sz / buoyancy.transform.localScale.z);
                                }
                                else
                                {
                                    Debug.Log("No Box Collider found");
                                }
                            }

                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.magnitude = br.ReadSingle();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.ypos = br.ReadSingle();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.CenterOfMassOffset = br.ReadSingle();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.dampCoeff = br.ReadSingle();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.SlicesX = br.ReadInt32();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.SlicesZ = br.ReadInt32();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.interpolation = br.ReadInt32();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.ChoppynessAffectsPosition = br.ReadBoolean();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.cvisible = br.ReadBoolean();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.ChoppynessFactor = br.ReadSingle();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.WindAffectsPosition = br.ReadBoolean();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.wvisible = br.ReadBoolean();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.WindFactor = br.ReadSingle();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.xAngleAddsSliding = br.ReadBoolean();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.svisible = br.ReadBoolean();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.slideFactor = br.ReadSingle();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.moreAccurate = br.ReadBoolean();
                            }
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.useFixedUpdate = br.ReadBoolean();
                            }
                            bool hasBoatController = false;
                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                hasBoatController = br.ReadBoolean();
                                if (hasBoatController)
                                {
                                    float res = 0;
                                    var   bc  = buoyancy.GetComponent <BoatController>();
                                    res = br.ReadSingle(); if (bc)
                                    {
                                        bc.m_FinalSpeed = res;
                                    }
                                    res = br.ReadSingle(); if (bc)
                                    {
                                        bc.m_accelerationTorqueFactor = res;
                                    }
                                    res = br.ReadSingle(); if (bc)
                                    {
                                        bc.m_InertiaFactor = res;
                                    }
                                    res = br.ReadSingle(); if (bc)
                                    {
                                        bc.m_turningFactor = res;
                                    }
                                    res = br.ReadSingle(); if (bc)
                                    {
                                        bc.m_turningTorqueFactor = res;
                                    }
                                }
                            }

                            if (br.BaseStream.Position != br.BaseStream.Length)
                            {
                                buoyancy.renderQueue = br.ReadInt32();
                            }

                            //try to asign a renderer for visibility checks if there is none assigned in the boyancy inspector.
                            if (buoyancy._renderer == null)
                            {
                                buoyancy._renderer = buoyancy.GetComponent <Renderer>();
                                if (!buoyancy._renderer)
                                {
                                    buoyancy._renderer = buoyancy.GetComponentInChildren <Renderer>();
                                }
                            }

                            EditorUtility.SetDirty(buoyancy);
                            if (hasBoatController)
                            {
                                var bc = buoyancy.GetComponent <BoatController>();
                                EditorUtility.SetDirty(bc);
                                if (!EditorApplication.isPlaying)
                                {
                                    EditorSceneManager.MarkSceneDirty(bc.gameObject.scene);
                                }
                            }

                            if (!EditorApplication.isPlaying)
                            {
                                EditorSceneManager.MarkSceneDirty(buoyancy.gameObject.scene);
                            }
                            return(true);
                        }
                    }
                    else
                    {
                        Debug.Log(preset + " does not exist..."); return(false);
                    }
                }
            }
        }
        else
        {
            Debug.Log("Cannot load this on runtime");
        }

        return(false);
    }
Exemplo n.º 4
0
    //saves a buoyancy preset
    public static void savePreset(Buoyancy buoyancy)
    {
        if (!Directory.Exists(presetPath))
        {
            Directory.CreateDirectory(presetPath);
        }
        string preset = EditorUtility.SaveFilePanel("Save buoyancy preset", presetPath, "", "buoyancy");

        if (preset != null)
        {
            if (preset.Length > 0)
            {
                using (BinaryWriter swr = new BinaryWriter(File.Open(preset, FileMode.Create))) {
                    //rigidbody parameters
                    if (rrig != null)
                    {
                        swr.Write((byte)1);                        //has Rigidbody
                        swr.Write((float)rrig.mass);               //float
                        swr.Write((float)rrig.drag);               //float
                        swr.Write((float)rrig.angularDrag);        //float
                        swr.Write((bool)rrig.useGravity);          //bool
                    }

                    //box collider parameters
                    if (col != null)
                    {
                        swr.Write((byte)1);                                      //has Boxcollider
                        swr.Write(col.center.x);                                 //float
                        swr.Write(col.center.y);                                 //float
                        swr.Write(col.center.z);                                 //float
                        swr.Write(col.size.x * buoyancy.transform.localScale.x); //float
                        swr.Write(col.size.y * buoyancy.transform.localScale.y); //float
                        swr.Write(col.size.z * buoyancy.transform.localScale.z); //float
                    }

                    //buoyancy parameters
                    swr.Write(buoyancy.magnitude);                 //float
                    swr.Write(buoyancy.ypos);                      //float
                    swr.Write(buoyancy.CenterOfMassOffset);        //float
                    swr.Write(buoyancy.dampCoeff);                 //float
                    swr.Write(buoyancy.SlicesX);                   //int
                    swr.Write(buoyancy.SlicesZ);                   //int
                    swr.Write(buoyancy.interpolation);             //int
                    swr.Write(buoyancy.ChoppynessAffectsPosition); //bool
                    swr.Write(buoyancy.cvisible);                  //bool
                    swr.Write(buoyancy.ChoppynessFactor);          //float
                    swr.Write(buoyancy.WindAffectsPosition);       //bool
                    swr.Write(buoyancy.wvisible);                  //bool
                    swr.Write(buoyancy.WindFactor);                //float
                    swr.Write(buoyancy.xAngleAddsSliding);         //bool
                    swr.Write(buoyancy.svisible);                  //bool
                    swr.Write(buoyancy.slideFactor);               //float
                    swr.Write(buoyancy.moreAccurate);              //bool
                    swr.Write(buoyancy.useFixedUpdate);            //bool

                    var bc = buoyancy.GetComponent <BoatController>();

                    //If the object has a boat controller attached write the properties.
                    //This is useful, because if you change the buoynacy settings the speed
                    //settings of the boat controller need tweaking again.
                    if (bc != null)
                    {
                        swr.Write(true);                          //bool
                        swr.Write(bc.m_FinalSpeed);               //float
                        swr.Write(bc.m_accelerationTorqueFactor); //float
                        swr.Write(bc.m_InertiaFactor);            //float
                        swr.Write(bc.m_turningFactor);            //float
                        swr.Write(bc.m_turningTorqueFactor);      //float
                    }
                    else
                    {
                        swr.Write(false);                        //bool
                    }

                    swr.Write(buoyancy.renderQueue);                    //int
                }
            }
        }
    }