Asset type that defines the surface properties of a Collider2D.

Inheritance: UnityEngine.Object
 private void Awake()
 {
     physicMaterial = new PhysicsMaterial2D ();
     physicMaterial.friction = 0.2f;
     physicMaterial.bounciness = 1.4f;
     GetComponent<Collider2D>().sharedMaterial = physicMaterial;
 }
        /// <summary>
        /// Read the data into the specified value.
        /// </summary>
        /// <param name="value">Value.</param>
        /// <param name="reader">Reader.</param>
        public override void ReadInto(object value, ISaveGameReader reader)
        {
            UnityEngine.PhysicsMaterial2D physicsMaterial2D = (UnityEngine.PhysicsMaterial2D)value;
            foreach (string property in reader.Properties)
            {
                switch (property)
                {
                case "bounciness":
                    physicsMaterial2D.bounciness = reader.ReadProperty <System.Single> ();
                    break;

                case "friction":
                    physicsMaterial2D.friction = reader.ReadProperty <System.Single> ();
                    break;

                case "name":
                    physicsMaterial2D.name = reader.ReadProperty <System.String> ();
                    break;

                case "hideFlags":
                    physicsMaterial2D.hideFlags = reader.ReadProperty <UnityEngine.HideFlags> ();
                    break;
                }
            }
        }
    /*Amount of force to subtract from the total, is a subtraction instead of simply assigning
      so that it could work with powerups that also influence the variable*/
    void Start()
    {
        PhysicsMaterial2D frictionMat = new PhysicsMaterial2D("Slippery");
        //Creates new material
        if (this.slippery)
            frictionMat.friction = 0f;
        else
            frictionMat.friction = 1f;
        //Sets its friction
        this.gameObject.collider2D.enabled = false;
        //Preps the collider
        this.gameObject.collider2D.sharedMaterial = frictionMat;
        //Sets the game object to have the new material
        this.gameObject.collider2D.enabled = true;
        //Resets the collider

        PlayerBallControl pbc = GameObject.FindWithTag ("Player").GetComponent<PlayerBallControl>();
        if (pbc != null)
        {
            restoreStopMultiplier = pbc.translationStoppingMultiplier;
            restoreForce = pbc.moveForce;
            restoreFriction = pbc.frictionCoefficient;
        }
        /*This material makes you slide, but does not influence human control (pressing a key)
         * The only way to influence how fast a player can stop or turn on the surface when wanted is
         * by controlling the player ball control script, it seems, and then the move force*/
    }
 public override void Reset()
 {
     gameObject = null;
     physicsMaterial2d = null;
     friction = null;
     everyFrame = false;
 }
Exemple #5
0
 static public int constructor(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         UnityEngine.PhysicsMaterial2D o;
         if (argc == 1)
         {
             o = new UnityEngine.PhysicsMaterial2D();
             pushValue(l, true);
             pushValue(l, o);
             return(2);
         }
         else if (argc == 2)
         {
             System.String a1;
             checkType(l, 2, out a1);
             o = new UnityEngine.PhysicsMaterial2D(a1);
             pushValue(l, true);
             pushValue(l, o);
             return(2);
         }
         return(error(l, "New object failed."));
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 public override void Reset()
 {
     gameObject = null;
     physicsMaterial2d = null;
     bounciness = null;
     everyFrame = false;
 }
 static public int set_bounciness(IntPtr l)
 {
     try {
                     #if DEBUG
         var    method     = System.Reflection.MethodBase.GetCurrentMethod();
         string methodName = GetMethodName(method);
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.BeginSample(methodName);
                     #else
         Profiler.BeginSample(methodName);
                     #endif
                     #endif
         UnityEngine.PhysicsMaterial2D self = (UnityEngine.PhysicsMaterial2D)checkSelf(l);
         float v;
         checkType(l, 2, out v);
         self.bounciness = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
             #if DEBUG
     finally {
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.EndSample();
                     #else
         Profiler.EndSample();
                     #endif
     }
             #endif
 }
 public static int constructor(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         UnityEngine.PhysicsMaterial2D o;
         if(argc==1){
             o=new UnityEngine.PhysicsMaterial2D();
             pushValue(l,o);
             return 1;
         }
         else if(argc==2){
             System.String a1;
             checkType(l,2,out a1);
             o=new UnityEngine.PhysicsMaterial2D(a1);
             pushValue(l,o);
             return 1;
         }
         LuaDLL.luaL_error(l,"New object failed.");
         return 0;
     }
     catch(Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return 0;
     }
 }
 public void CopyParametersFrom( Collider2D origin )
 {
     isTrigger = origin.isTrigger;
     offset = origin.offset;
     sharedMaterial = origin.sharedMaterial;
     usedByEffector = origin.usedByEffector;
     enabled = origin.enabled;
 }
 /// <summary>
 /// Write the specified value using the writer.
 /// </summary>
 /// <param name="value">Value.</param>
 /// <param name="writer">Writer.</param>
 public override void Write(object value, ISaveGameWriter writer)
 {
     UnityEngine.PhysicsMaterial2D physicsMaterial2D = (UnityEngine.PhysicsMaterial2D)value;
     writer.WriteProperty("bounciness", physicsMaterial2D.bounciness);
     writer.WriteProperty("friction", physicsMaterial2D.friction);
     writer.WriteProperty("name", physicsMaterial2D.name);
     writer.WriteProperty("hideFlags", physicsMaterial2D.hideFlags);
 }
 public StateFalling(Fsm fsm)
     : base(fsm)
 {
     _material = Resources.Load<PhysicsMaterial2D>("NoFriction");
     _collider = Fsm.gameObject.GetComponent<Collider2D>(); _collider.sharedMaterial = null;
     _locomotion = Fsm.gameObject.GetOrAdd<ManagerLocomotion>(); _locomotion.Rigidbody.gravityScale = 11f;
     _input = Fsm.gameObject.GetComponent<IManagerInput>();
 }
Exemple #12
0
 void Start()
 {
     GameObject gmHold = GameObject.Find ("GameManager");
             physMan = gmHold.GetComponent<PhysicsManipulation> ();
             friction = gmHold.GetComponent<BoxCollider2D> ().sharedMaterial;
             noFriction = gmHold.GetComponent<CircleCollider2D> ().sharedMaterial;
             startingPos = gameObject.transform.position;
 }
Exemple #13
0
    void Start()
    {
        bounceCount = 0;

        ballRigidBody = gameObject.GetComponent<Rigidbody2D>();
        ballCollider = gameObject.GetComponent<CircleCollider2D>();
        bounceMat = ballCollider.sharedMaterial;
    }
        private void createWithCollider2d(Collider2D coll)
        {
            b2FixtureDef      fixtureDef = new b2FixtureDef();
            PhysicsMaterial2D material   = coll.sharedMaterial;

            if (material != null)
            {
                fixtureDef.restitution = material.bounciness;
                fixtureDef.friction    = material.friction;
            }
            fixtureDef.isSensor = coll.isTrigger;

            if (coll is BoxCollider2D)
            {
                BoxCollider2D  boxColl = coll as BoxCollider2D;
                b2PolygonShape s       = b2PolygonShape.AsOrientedBox(boxColl.size.x * 0.5f,
                                                                      boxColl.size.y * 0.5f,
                                                                      new b2Vec2(boxColl.offset.x, boxColl.offset.y),
                                                                      0 /*transform.eulerAngles.z*Mathf.Deg2Rad*/);
                scaleShape(s);
                fixtureDef.shape   = s;
                _fixtureDict[coll] = new b2Fixture[] { _body.CreateFixture(fixtureDef) };
            }
            else if (coll is CircleCollider2D)
            {
                CircleCollider2D circleColl = coll as CircleCollider2D;
                b2CircleShape    s          = new b2CircleShape(circleColl.radius);
                s.SetLocalPosition(new b2Vec2(circleColl.offset.x, circleColl.offset.y));
                scaleShape(s);
                fixtureDef.shape   = s;
                _fixtureDict[coll] = new b2Fixture[] { _body.CreateFixture(fixtureDef) };
            }
            else if (coll is PolygonCollider2D)
            {
                int i, j;
                PolygonCollider2D polyColl = coll as PolygonCollider2D;

                List <b2Fixture> fixtureList = new List <b2Fixture>();
                int pathCount = polyColl.pathCount;
                for (i = 0; i < pathCount; i++)
                {
                    Vector2[] path     = polyColl.GetPath(i);
                    b2Vec2[]  vertices = new b2Vec2[path.Length];
                    for (j = 0; j < path.Length; j++)
                    {
                        vertices[j] = new b2Vec2(path[j].x, path[j].y);
                    }
                    b2Separator sep      = new b2Separator();
                    b2Fixture[] fixtures = sep.Separate(_body, fixtureDef, vertices, 100, polyColl.offset.x, polyColl.offset.y);             //必须放大100倍进行计算
                    for (j = 0; j < fixtures.Length; j++)
                    {
                        scaleShape(fixtures[j].GetShape());
                    }
                    fixtureList.AddRange(fixtures);
                }
                _fixtureDict[coll] = fixtureList.ToArray();
            }
        }
Exemple #15
0
    static public int set_bounciness(IntPtr l)
    {
        UnityEngine.PhysicsMaterial2D o = (UnityEngine.PhysicsMaterial2D)checkSelf(l);
        float v;

        checkType(l, 2, out v);
        o.bounciness = v;
        return(0);
    }
Exemple #16
0
    static public int set_friction(IntPtr l)
    {
        UnityEngine.PhysicsMaterial2D o = (UnityEngine.PhysicsMaterial2D)checkSelf(l);
        float v;

        checkType(l, 2, out v);
        o.friction = v;
        return(0);
    }
Exemple #17
0
 private void Awake()
 {
     bricksContainer = new GameObject("Bricks").transform;
     brickTexture = TextureE.whitePixel;
     brickTexture.Resize(brickWidth, brickHeight);
     brickSprite = Sprite.Create(brickTexture, new Rect(0, 0, brickWidth, brickHeight), new Vector2(0.5f, 0.5f));
     material = new PhysicsMaterial2D {name = "Bouncy", bounciness = 1, friction = 0};
     GeneratePaddle();
     GenerateBorders();
 }
Exemple #18
0
 static public int get_friction(IntPtr l)
 {
     try {
         UnityEngine.PhysicsMaterial2D self = (UnityEngine.PhysicsMaterial2D)checkSelf(l);
         pushValue(l, true);
         pushValue(l, self.friction);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Exemple #19
0
 static public int get_friction(IntPtr l)
 {
     try {
         UnityEngine.PhysicsMaterial2D self = (UnityEngine.PhysicsMaterial2D)checkSelf(l);
         pushValue(l, self.friction);
         return(1);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
        public Breakout()
        {
            bricksContainer = new GameObject("Bricks").transform;

            // Generate texture and sprite for bricks, paddle and ball
            texture = Texture2D.whiteTexture;
            sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.width),
                pivot: new Vector2(0.5f, 0.5f),
                pixelsPerUnit: texture.width);

            // Bouncy material for walls, paddle and everything else
            bouncyMaterial = new PhysicsMaterial2D {name = "Bouncy", bounciness = 1, friction = 0};
        }
Exemple #21
0
 static public int set_friction(IntPtr l)
 {
     try {
         UnityEngine.PhysicsMaterial2D self = (UnityEngine.PhysicsMaterial2D)checkSelf(l);
         float v;
         checkType(l, 2, out v);
         self.friction = v;
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Exemple #22
0
 static public int set_friction(IntPtr l)
 {
     try {
         UnityEngine.PhysicsMaterial2D self = (UnityEngine.PhysicsMaterial2D)checkSelf(l);
         float v;
         checkType(l, 2, out v);
         self.friction = v;
         return(0);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
Exemple #23
0
 static void Collider2D_sharedMaterial(JSVCall vc)
 {
     if (vc.bGet)
     {
         UnityEngine.Collider2D _this = (UnityEngine.Collider2D)vc.csObj;
         var result = _this.sharedMaterial;
         JSMgr.datax.setObject((int)JSApi.SetType.Rval, result);
     }
     else
     {
         UnityEngine.PhysicsMaterial2D arg0  = (UnityEngine.PhysicsMaterial2D)JSMgr.datax.getObject((int)JSApi.GetType.Arg);
         UnityEngine.Collider2D        _this = (UnityEngine.Collider2D)vc.csObj;
         _this.sharedMaterial = arg0;
     }
 }
 static void PhysicsMaterial2D_friction(JSVCall vc)
 {
     if (vc.bGet)
     {
         UnityEngine.PhysicsMaterial2D _this = (UnityEngine.PhysicsMaterial2D)vc.csObj;
         var result = _this.friction;
         JSApi.setSingle((int)JSApi.SetType.Rval, (System.Single)(result));
     }
     else
     {
         System.Single arg0 = (System.Single)JSApi.getSingle((int)JSApi.GetType.Arg);
         UnityEngine.PhysicsMaterial2D _this = (UnityEngine.PhysicsMaterial2D)vc.csObj;
         _this.friction = arg0;
     }
 }
Exemple #25
0
    static int QPYX_set_sharedMaterial_YXQP(IntPtr L_YXQP)
    {
        object QPYX_o_YXQP = null;

        try
        {
            QPYX_o_YXQP = ToLua.ToObject(L_YXQP, 1);                        UnityEngine.Collider2D QPYX_obj_YXQP = (UnityEngine.Collider2D)QPYX_o_YXQP;
            UnityEngine.PhysicsMaterial2D QPYX_arg0_YXQP = (UnityEngine.PhysicsMaterial2D)ToLua.CheckObject(L_YXQP, 2, typeof(UnityEngine.PhysicsMaterial2D));
            QPYX_obj_YXQP.sharedMaterial = QPYX_arg0_YXQP;
            return(0);
        }
        catch (Exception QPYX_e_YXQP)            {
            return(LuaDLL.toluaL_exception(L_YXQP, QPYX_e_YXQP, QPYX_o_YXQP, "attempt to index sharedMaterial on a nil value"));
        }
    }
Exemple #26
0
    static int QPYX_get_sharedMaterial_YXQP(IntPtr L_YXQP)
    {
        object QPYX_o_YXQP = null;

        try
        {
            QPYX_o_YXQP = ToLua.ToObject(L_YXQP, 1);                        UnityEngine.Collider2D QPYX_obj_YXQP = (UnityEngine.Collider2D)QPYX_o_YXQP;
            UnityEngine.PhysicsMaterial2D QPYX_ret_YXQP = QPYX_obj_YXQP.sharedMaterial;
            ToLua.PushSealed(L_YXQP, QPYX_ret_YXQP);
            return(1);
        }
        catch (Exception QPYX_e_YXQP)            {
            return(LuaDLL.toluaL_exception(L_YXQP, QPYX_e_YXQP, QPYX_o_YXQP, "attempt to index sharedMaterial on a nil value"));
        }
    }
        /// <summary>
        /// Adds a BoxCollider2D to a GameObject
        /// </summary>
        /// <param name="gameObject">GameObject to add the collider</param>
        /// <param name="obj">MapObject which properties will be used to generate this collider</param>
        /// <param name="isTrigger">True for Trigger Collider, false otherwise</param>
        /// <param name="physicsMaterial">PhysicsMaterial2D to be set to the collider</param>
        /// <param name="zDepth">Z Depth of the collider</param>
        /// <param name="createRigidbody">True to attach a Rigidbody to the created collider</param>
        /// <param name="rigidbodyIsKinematic">Sets if the attached rigidbody is kinematic or not</param>
        public static void AddBoxCollider2D(Map map, GameObject gameObject, MapObject obj, bool isTrigger = false, PhysicsMaterial2D physicsMaterial = null, float zDepth = 0, bool createRigidbody = false, bool rigidbodyIsKinematic = true)
        {
            // Orthogonal and Staggered maps can use BoxCollider, Isometric maps must use polygon collider
            if (map.MapRenderParameter.Orientation != X_UniTMX.Orientation.Isometric)
            {
                BoxCollider2D bx = gameObject.AddComponent<BoxCollider2D>();
                bx.isTrigger = isTrigger || obj.Type.Equals(Map.Object_Type_Trigger);
            #if UNITY_5
                bx.offset = new Vector2(obj.Bounds.width / 2.0f, -obj.Bounds.height / 2.0f);
            #else
                bx.center = new Vector2(obj.Bounds.width / 2.0f, -obj.Bounds.height / 2.0f);
            #endif
                bx.size = new Vector2(obj.Bounds.width, obj.Bounds.height);
                if (physicsMaterial != null)
                    bx.sharedMaterial = physicsMaterial;
            }
            else if (map.MapRenderParameter.Orientation == X_UniTMX.Orientation.Isometric)
            {
                PolygonCollider2D pc = gameObject.AddComponent<PolygonCollider2D>();
                pc.isTrigger = isTrigger || obj.Type.Equals(Map.Object_Type_Trigger);
                Vector2[] points = new Vector2[4];
                points[0] = map.TiledPositionToWorldPoint(obj.Bounds.xMin - obj.Bounds.x, obj.Bounds.yMax - obj.Bounds.y);
                points[1] = map.TiledPositionToWorldPoint(obj.Bounds.xMin - obj.Bounds.x, obj.Bounds.yMin - obj.Bounds.y);
                points[2] = map.TiledPositionToWorldPoint(obj.Bounds.xMax - obj.Bounds.x, obj.Bounds.yMin - obj.Bounds.y);
                points[3] = map.TiledPositionToWorldPoint(obj.Bounds.xMax - obj.Bounds.x, obj.Bounds.yMax - obj.Bounds.y);
                points[0].x -= map.MapRenderParameter.Width / 2.0f;
                points[1].x -= map.MapRenderParameter.Width / 2.0f;
                points[2].x -= map.MapRenderParameter.Width / 2.0f;
                points[3].x -= map.MapRenderParameter.Width / 2.0f;
                pc.SetPath(0, points);
                if (physicsMaterial != null)
                    pc.sharedMaterial = physicsMaterial;
            }

            if (createRigidbody)
            {
                Rigidbody2D r = gameObject.AddComponent<Rigidbody2D>();
                r.isKinematic = rigidbodyIsKinematic;
            }

            if (obj.Rotation != 0)
                gameObject.transform.localRotation = Quaternion.AngleAxis(obj.Rotation, Vector3.forward);

            ApplyCustomProperties(gameObject, obj);

            // Link this collider to the MapObject
            obj.LinkedGameObject = gameObject;
        }
    static int set_sharedMaterial(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UnityEngine.Collider2D        obj  = (UnityEngine.Collider2D)o;
            UnityEngine.PhysicsMaterial2D arg0 = (UnityEngine.PhysicsMaterial2D)ToLua.CheckUnityObject(L, 2, typeof(UnityEngine.PhysicsMaterial2D));
            obj.sharedMaterial = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index sharedMaterial on a nil value" : e.Message));
        }
    }
    static int get_sharedMaterial(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UnityEngine.Collider2D        obj = (UnityEngine.Collider2D)o;
            UnityEngine.PhysicsMaterial2D ret = obj.sharedMaterial;
            ToLua.Push(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index sharedMaterial on a nil value" : e.Message));
        }
    }
    void OnCollisionEnter2D(Collision2D obj)
    {
        //if the game object that collides with the block's collider has the tag "bullet"
        if (obj.gameObject.tag == "bullet") {
            int type = obj.gameObject.GetComponent<Bullet_Script>().GetBulletType(); //get the type of the bullet
            if(type < 3)
            {
                current = mats[type]; //set the variable current based on the type of the bullet
                col.sharedMaterial = current; //set the material of the block to the current material
                col.enabled = false; //disable block's collider
                col.enabled = true; //then re-enable block's collider to make changes take effect
                sp.sprite = sprites[type];
            }

            Destroy(obj.gameObject); //destroy the object that collided with the platform
        }
    }
 static public int constructor(IntPtr l)
 {
     try {
                     #if DEBUG
         var    method     = System.Reflection.MethodBase.GetCurrentMethod();
         string methodName = GetMethodName(method);
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.BeginSample(methodName);
                     #else
         Profiler.BeginSample(methodName);
                     #endif
                     #endif
         int argc = LuaDLL.lua_gettop(l);
         UnityEngine.PhysicsMaterial2D o;
         if (argc == 1)
         {
             o = new UnityEngine.PhysicsMaterial2D();
             pushValue(l, true);
             pushValue(l, o);
             return(2);
         }
         else if (argc == 2)
         {
             System.String a1;
             checkType(l, 2, out a1);
             o = new UnityEngine.PhysicsMaterial2D(a1);
             pushValue(l, true);
             pushValue(l, o);
             return(2);
         }
         return(error(l, "New object failed."));
     }
     catch (Exception e) {
         return(error(l, e));
     }
             #if DEBUG
     finally {
                     #if UNITY_5_5_OR_NEWER
         UnityEngine.Profiling.Profiler.EndSample();
                     #else
         Profiler.EndSample();
                     #endif
     }
             #endif
 }
 static public int constructor(IntPtr l)
 {
     LuaDLL.lua_remove(l, 1);
     UnityEngine.PhysicsMaterial2D o;
     if (matchType(l, 1))
     {
         o = new UnityEngine.PhysicsMaterial2D();
         pushObject(l, o);
         return(1);
     }
     else if (matchType(l, 1, typeof(System.String)))
     {
         System.String a1;
         checkType(l, 1, out a1);
         o = new UnityEngine.PhysicsMaterial2D(a1);
         pushObject(l, o);
         return(1);
     }
     LuaDLL.luaL_error(l, "New object failed.");
     return(0);
 }
Exemple #33
0
    static public int constructor(IntPtr l)
    {
        int argc = LuaDLL.lua_gettop(l);

        UnityEngine.PhysicsMaterial2D o;
        if (argc == 1)
        {
            o = new UnityEngine.PhysicsMaterial2D();
            pushObject(l, o);
            return(1);
        }
        else if (argc == 2)
        {
            System.String a1;
            checkType(l, 2, out a1);
            o = new UnityEngine.PhysicsMaterial2D(a1);
            pushObject(l, o);
            return(1);
        }
        LuaDLL.luaL_error(l, "New object failed.");
        return(0);
    }
    public void init(EnergyStream energy, string spriteName, float damageRatio, int bounces, float lifespan, float speed, Dictionary<string, List<ProjectileBehavior>> behaviors)
    {
        _energy = energy;
        _damageRatio = damageRatio;
        _lifespan = lifespan;
        _speed = speed;
        _behaviors = new Dictionary<string, List<ProjectileBehavior>>();

        foreach (string key in behaviors.Keys)
        {
            foreach (ProjectileBehavior behavior in behaviors[key])
            {
                addBehavior(key, behavior);
            }
        }

        setSprite(spriteName);

        if(gameObject != null)
        {
            if(gameObject.GetComponent<SpriteRenderer>() != null)
            {
                setSpriteRenderer(gameObject.GetComponent<SpriteRenderer>());
            }
            else
            {
                setSpriteRenderer(gameObject.AddComponent<SpriteRenderer>());
            }

            if (gameObject.GetComponent<CircleCollider2D>() != null)
            {
                setCollider(gameObject.GetComponent<CircleCollider2D>());
            }
            else
            {
                setCollider(gameObject.AddComponent<CircleCollider2D>());
            }

            if(gameObject.GetComponent<Rigidbody2D>() != null)
            {
                setRigidbody(gameObject.GetComponent<Rigidbody2D>());
            }
            else
            {
                setRigidbody(gameObject.AddComponent<Rigidbody2D>());
            }
        }

        //Woo hard coding for now.
        if(_collider != null)
        {
            PhysicsMaterial2D projectilePhysicsMaterial = new PhysicsMaterial2D("Projectile");
            projectilePhysicsMaterial.bounciness = 1;
            projectilePhysicsMaterial.friction = 0;
            _collider.sharedMaterial = projectilePhysicsMaterial;
            //Separate physics will need to be written to handle this.
            //_collider.isTrigger = true;   //It is a trigger because it passes through other Projectiles.

            _collider.enabled = false;
            _collider.enabled = true;
        }

        if(_rigidbody != null)
        {
            _rigidbody.angularDrag = 0;
            _rigidbody.gravityScale = 0;
            _rigidbody.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
            _rigidbody.interpolation = RigidbodyInterpolation2D.Interpolate;
        }
    }
        /// <summary>
        /// Generate 2D Colliders based on Tile Collisions
        /// </summary>
        /// <param name="isTrigger">True for Trigger Collider, false otherwise</param>
        /// <param name="generateClosedPolygon">True to generate a Polygon Collider. False will generate Edge Collider.</param>
        /// <param name="tag">Tag for the generated GameObjects</param>
        /// <param name="physicsLayer">Physics Layer for the generated GameObjects</param>
        /// <param name="physicsMaterial2D">Physics Material for 2D collider</param>
        /// <param name="zDepth">Z Depth of the collider.</param>
        /// <param name="colliderWidth">Width of the collider, in Units</param>
        /// <param name="innerCollision">If true, calculate normals facing the anchor of the collider (inside collisions), else, outside collisions.</param>
        /// <param name="simpleTileObjectCalculation">true to generate simplified tile collisions</param>
        /// <param name="clipperArcTolerance">Clipper arc angle tolerance</param>
        /// <param name="clipperMiterLimit">Clipper limit for Miter join type</param>
        /// <param name="clipperJoinType">Clipper join type</param>
        /// <param name="clipperEndType">Clipper Polygon end type</param>
        /// <param name="clipperDeltaOffset">Clipper delta offset</param>
        /// <returns></returns>
        public static GameObject[] GenerateTileCollisions2D(this Map map, bool isTrigger = false, bool generateClosedPolygon = true,
			string tag = "Untagged", int physicsLayer = 0, PhysicsMaterial2D physicsMaterial2D = null,
			float zDepth = 0, bool simpleTileObjectCalculation = true,
			double clipperArcTolerance = 0.25, double clipperMiterLimit = 2.0,
			ClipperLib.JoinType clipperJoinType = ClipperLib.JoinType.jtRound,
			ClipperLib.EndType clipperEndType = ClipperLib.EndType.etClosedPolygon,
			float clipperDeltaOffset = 0)
        {
            List<GameObject> tileCollisions = new List<GameObject>();
            // Iterate over each Tile Layer, grab all TileObjects inside this layer and use their Paths with ClipperLib to generate one polygon collider
            foreach (var layer in map.Layers)
            {
                if (layer is TileLayer)
                {
                    tileCollisions.AddRange(GenerateTileCollision2DFromLayer(map, layer as TileLayer, isTrigger, generateClosedPolygon, tag, physicsLayer, physicsMaterial2D, zDepth, simpleTileObjectCalculation, clipperArcTolerance, clipperMiterLimit, clipperJoinType, clipperEndType, clipperDeltaOffset));
                }
            }

            return tileCollisions.ToArray();
        }
        public static GameObject[] GenerateColliders2DFromLayer(this Map map, string objectLayerName, bool collidersAreTrigger = false, string tag = "Untagged", int physicsLayer = 0, PhysicsMaterial2D physicsMaterial = null, float collidersZDepth = 0)
        {
            MapObjectLayer objectLayer = map.GetObjectLayer(objectLayerName);
            if (objectLayer != null)
            {
                return map.GenerateColliders2DFromLayer(objectLayer, collidersAreTrigger, tag, physicsLayer, physicsMaterial, collidersZDepth);
            }
            else
            {
                Debug.LogWarning("There's no Layer \"" + objectLayerName + "\" in tile map.");
            }

            return null;
        }
        public static void AddEllipseCollider2D(Map map, GameObject gameObject, MapObject obj, bool isTrigger = false, PhysicsMaterial2D physicsMaterial = null, float zDepth = 0, bool createRigidbody = false, bool rigidbodyIsKinematic = true)
        {
            if (map.MapRenderParameter.Orientation != X_UniTMX.Orientation.Isometric && obj.Bounds.width == obj.Bounds.height)
            {
                CircleCollider2D cc = gameObject.AddComponent<CircleCollider2D>();
                cc.isTrigger = isTrigger || obj.Type.Equals(Map.Object_Type_Trigger);

                gameObject.transform.localPosition = map.TiledPositionToWorldPoint(obj.Bounds.x, obj.Bounds.y, zDepth);
            #if UNITY_5
                cc.offset = new Vector2(obj.Bounds.width / 2.0f, -obj.Bounds.height / 2.0f);
            #else
                cc.center = new Vector2(obj.Bounds.width / 2.0f, -obj.Bounds.height / 2.0f);
            #endif

                cc.radius = obj.Bounds.width / 2.0f;
                if (physicsMaterial != null)
                    cc.sharedMaterial = physicsMaterial;

            }
            else
            {
                ApproximateEllipse2D(map, gameObject, obj, isTrigger, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);
            }

            if (createRigidbody)
            {
                Rigidbody2D r = gameObject.AddComponent<Rigidbody2D>();
                r.isKinematic = rigidbodyIsKinematic;
            }

            if (obj.Rotation != 0)
                gameObject.transform.localRotation = Quaternion.AngleAxis(obj.Rotation, Vector3.forward);

            ApplyCustomProperties(gameObject, obj);

            // Link this collider to the MapObject
            obj.LinkedGameObject = gameObject;
        }
 public PhysicsMaterial2D()
 {
     PhysicsMaterial2D.Internal_Create(this, null);
 }
 private static extern void Internal_Create([Writable] PhysicsMaterial2D mat, string name);
 public PhysicsMaterial2D(string name)
 {
     PhysicsMaterial2D.Create_Internal(this, name);
 }
        public static GameObject[] GenerateColliders2DFromLayer(this Map map, MapObjectLayer objectLayer, bool collidersAreTrigger = false, string tag = "Untagged", int physicsLayer = 0, PhysicsMaterial2D physicsMaterial = null, float collidersZDepth = 0)
        {
            if (objectLayer != null)
            {
                List<GameObject> generatedGameObjects = new List<GameObject>();

                List<MapObject> colliders = objectLayer.Objects;
                foreach (MapObject colliderObjMap in colliders)
                {
                    // This function should not try to generate a collider where a prefab will be generated!
                    if (colliderObjMap.HasProperty(Map.Property_PrefabName))
                        continue;
                    // Also, do not generate collider for a TileObject
                    if (colliderObjMap.GID > 0)
                        continue;
                    GameObject newColliderObject = null;
                    if (colliderObjMap.Type.Equals(Map.Object_Type_NoCollider) == false)
                    {
                        newColliderObject = map.GenerateCollider2D(colliderObjMap, collidersAreTrigger, tag, physicsLayer, physicsMaterial, collidersZDepth);
                    }

                    if (newColliderObject) generatedGameObjects.Add(newColliderObject);
                }

                return generatedGameObjects.ToArray();
            }

            return null;
        }
        public void UpdateColliderSettings(bool isTrigger, PhysicsMaterial2D material)
        {
            for (var i = Colliders.Count - 1; i >= 0; i--)
            {
                var collider = Colliders[i];

                if (collider != null)
                {
                    collider.isTrigger      = isTrigger;
                    collider.sharedMaterial = material;
                }
            }
        }
 public static void AddCollider2D(Map map, GameObject gameObject, MapObject obj, bool isTrigger = false, PhysicsMaterial2D physicsMaterial = null, float zDepth = 0, bool createRigidbody = false, bool rigidbodyIsKinematic = true)
 {
     switch (obj.ObjectType)
     {
         case ObjectType.Box:
             AddBoxCollider2D(map, gameObject, obj, isTrigger, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);
             break;
         case ObjectType.Ellipse:
             AddEllipseCollider2D(map, gameObject, obj, isTrigger, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);
             break;
         case ObjectType.Polygon:
             AddPolygonCollider2D(map, gameObject, obj, isTrigger, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);
             break;
         case ObjectType.Polyline:
             AddPolylineCollider2D(map, gameObject, obj, isTrigger, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);
             break;
     }
 }
Exemple #44
0
 static public int get_friction(IntPtr l)
 {
     UnityEngine.PhysicsMaterial2D o = (UnityEngine.PhysicsMaterial2D)checkSelf(l);
     pushValue(l, o.friction);
     return(1);
 }
        private static void ApproximateEllipse2D(Map map, GameObject newCollider, MapObject obj, bool isTrigger = false, PhysicsMaterial2D physicsMaterial = null, float zDepth = 0, bool createRigidbody = false, bool rigidbodyIsKinematic = true)
        {
            // since there's no "EllipseCollider2D", we must create one by approximating a polygon collider
            newCollider.transform.localPosition = map.TiledPositionToWorldPoint(obj.Bounds.x, obj.Bounds.y, zDepth);

            PolygonCollider2D polygonCollider = newCollider.AddComponent<PolygonCollider2D>();

            polygonCollider.isTrigger = isTrigger || obj.Type.Equals(Map.Object_Type_Trigger);

            int segments = XUniTMXConfiguration.Instance.EllipsoideColliderApproximationFactor;

            // Segments per quadrant
            int incFactor = Mathf.FloorToInt(segments / 4.0f);
            float minIncrement = 2 * Mathf.PI / (incFactor * segments / 2.0f);
            int currentInc = 0;
            // grow represents if we are going right on x-axis (true) or left (false)
            bool grow = true;

            Vector2[] points = new Vector2[segments];
            // Ellipsoide center
            Vector2 center = new Vector2(obj.Bounds.width / 2.0f, obj.Bounds.height / 2.0f);

            float r = 0;
            float angle = 0;
            for (int i = 0; i < segments; i++)
            {
                // Calculate radius at each point
                angle += currentInc * minIncrement;

                r = obj.Bounds.width * obj.Bounds.height / Mathf.Sqrt(Mathf.Pow(obj.Bounds.height * Mathf.Cos(angle), 2) + Mathf.Pow(obj.Bounds.width * Mathf.Sin(angle), 2)) / 2.0f;
                // Define the point localization using the calculated radius, angle and center
                points[i] = r * new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) + center;

                points[i] = map.TiledPositionToWorldPoint(points[i].x, points[i].y);

                // Offset points where needed
                if (map.MapRenderParameter.Orientation == X_UniTMX.Orientation.Isometric)
                    points[i].x -= map.MapRenderParameter.Width / 2.0f;
                if (map.MapRenderParameter.Orientation == X_UniTMX.Orientation.Staggered)
                    points[i].y *= map.MapRenderParameter.TileWidth / (float)map.MapRenderParameter.TileHeight * 2.0f;
                if (map.MapRenderParameter.Orientation == X_UniTMX.Orientation.Hexagonal)
                {
                    points[i].y *= map.MapRenderParameter.TileWidth / (float)map.MapRenderParameter.TileHeight * 2.0f;
                }

                // if we are "growing", increment the angle, else, start decrementing it to close the polygon
                if (grow)
                    currentInc++;
                else
                    currentInc--;
                if (currentInc > incFactor - 1 || currentInc < 1)
                    grow = !grow;

                // POG :P -> Orthogonal and Staggered Isometric generated points are slightly offset on Y
                if (map.MapRenderParameter.Orientation != X_UniTMX.Orientation.Isometric)
                {
                    if (i < 1 || i == segments / 2 - 1)
                        points[i].y -= obj.Bounds.height / 20.0f;
                    if (i >= segments - 1 || i == segments / 2)
                        points[i].y += obj.Bounds.height / 20.0f;
                }
            }

            polygonCollider.SetPath(0, points);

            if (physicsMaterial != null)
                polygonCollider.sharedMaterial = physicsMaterial;
        }
Exemple #46
0
 static public int get_bounciness(IntPtr l)
 {
     UnityEngine.PhysicsMaterial2D o = (UnityEngine.PhysicsMaterial2D)checkSelf(l);
     pushValue(l, o.bounciness);
     return(1);
 }
 public PhysicsMaterial2D()
 {
     PhysicsMaterial2D.Create_Internal(this, null);
 }
        /// <summary>
        /// Generate Colliders based on Tile Collisions
        /// </summary>
        /// <param name="used2DColider">True to generate a 2D collider, false to generate a 3D collider.</param>
        /// <param name="isTrigger">True for Trigger Collider, false otherwise</param>
        /// <param name="generateClosedPolygon">True to generate a Polygon Collider. False will generate Edge Collider.</param>
        /// <param name="tag">Tag for the generated GameObjects</param>
        /// <param name="physicsLayer">Physics Layer for the generated GameObjects</param>
        /// <param name="physicsMaterial3D">Physics Material for 3D collider</param>
        /// <param name="physicsMaterial2D">Physics Material for 2D collider</param>
        /// <param name="zDepth">Z Depth of the collider.</param>
        /// <param name="colliderWidth">Width of the collider, in Units</param>
        /// <param name="innerCollision">If true, calculate normals facing the anchor of the collider (inside collisions), else, outside collisions.</param>
        /// <param name="simpleTileObjectCalculation">true to generate simplified tile collisions</param>
        /// <param name="clipperArcTolerance">Clipper arc angle tolerance</param>
        /// <param name="clipperMiterLimit">Clipper limit for Miter join type</param>
        /// <param name="clipperJoinType">Clipper join type</param>
        /// <param name="clipperEndType">Clipper Polygon end type</param>
        /// <param name="clipperDeltaOffset">Clipper delta offset</param>
        /// <returns>A GameObject containing all generated mapObjects</returns>
        public static GameObject[] GenerateTileCollisions(this Map map, bool used2DColider = true, bool isTrigger = false, bool generateClosedPolygon = true,
			string tag = "Untagged", int physicsLayer = 0, PhysicMaterial physicsMaterial3D = null, PhysicsMaterial2D physicsMaterial2D = null,
			float zDepth = 0, float colliderWidth = 1, bool innerCollision = false,
			bool simpleTileObjectCalculation = true,
			double clipperArcTolerance = 0.25, double clipperMiterLimit = 2.0,
			ClipperLib.JoinType clipperJoinType = ClipperLib.JoinType.jtRound,
			ClipperLib.EndType clipperEndType = ClipperLib.EndType.etClosedPolygon,
			float clipperDeltaOffset = 0)
        {
            if (used2DColider)
                return GenerateTileCollisions2D(map, isTrigger, generateClosedPolygon, tag, physicsLayer, physicsMaterial2D, zDepth, simpleTileObjectCalculation, clipperArcTolerance, clipperMiterLimit, clipperJoinType, clipperEndType, clipperDeltaOffset);
            else
                return GenerateTileCollisions3D(map, isTrigger, generateClosedPolygon, tag, physicsLayer, physicsMaterial3D, zDepth, colliderWidth, innerCollision, simpleTileObjectCalculation, clipperArcTolerance, clipperMiterLimit, clipperJoinType, clipperEndType, clipperDeltaOffset);
        }
 private static extern void Create_Internal([Writable] PhysicsMaterial2D scriptMaterial, string name);
    void Awake()
    {
        //set a new default physics material if not is assigned in the editor
        if (defaultPhysicsMaterial == null)
            defaultPhysicsMaterial = new PhysicsMaterial2D();

        //Clear all lists for the simulation
        mBodies.Clear();
        trackedColliders.Clear();
        TrackedColliderDictionary.Clear();
        mJoints.Clear();

        //start the collider tracker cleanup coroutine (will run as long as the simulation is running)
        //removes any null values from the collider tracker.
        StartCoroutine(CleanUpColliderTracker());
    }
Exemple #51
0
 private static void Internal_Create([Writable] PhysicsMaterial2D mat, string name)
 {
     throw new NotImplementedException("‚È‚É‚±‚ê");
 }
    static int _CreatePhysicsMaterial2D(IntPtr L)
    {
        int count = LuaDLL.lua_gettop(L);

        if (count == 0)
        {
            PhysicsMaterial2D obj = new PhysicsMaterial2D();
            LuaScriptMgr.Push(L, obj);
            return 1;
        }
        else if (count == 1)
        {
            string arg0 = LuaScriptMgr.GetString(L, 1);
            PhysicsMaterial2D obj = new PhysicsMaterial2D(arg0);
            LuaScriptMgr.Push(L, obj);
            return 1;
        }
        else
        {
            LuaDLL.luaL_error(L, "invalid arguments to method: PhysicsMaterial2D.New");
        }

        return 0;
    }
 public PhysicsMaterial2D(string name)
 {
     PhysicsMaterial2D.Internal_Create(this, name);
 }
        public static GameObject[] GenerateTileCollision2DFromLayer(
			this Map map, string layer,
			bool isTrigger = false, bool generateClosedPolygon = true, string tag = "Untagged",
			int physicsLayer = 0, PhysicsMaterial2D physicsMaterial = null, float zDepth = 1,
			bool simpleTileObjectCalculation = true,
			double clipperArcTolerance = 0.25, double clipperMiterLimit = 2.0,
			ClipperLib.JoinType clipperJoinType = ClipperLib.JoinType.jtRound,
			ClipperLib.EndType clipperEndType = ClipperLib.EndType.etClosedPolygon,
			float clipperDeltaOffset = 0)
        {
            return GenerateTileCollision2DFromLayer(map, map.GetTileLayer(layer), isTrigger, generateClosedPolygon, tag, physicsLayer, physicsMaterial, zDepth, simpleTileObjectCalculation, clipperArcTolerance, clipperMiterLimit, clipperJoinType, clipperEndType, clipperDeltaOffset);
        }
 /// <summary>
 /// Read the data using the reader.
 /// </summary>
 /// <param name="reader">Reader.</param>
 public override object Read(ISaveGameReader reader)
 {
     UnityEngine.PhysicsMaterial2D physicsMaterial2D = new UnityEngine.PhysicsMaterial2D();
     ReadInto(physicsMaterial2D, reader);
     return(physicsMaterial2D);
 }
        public static GameObject[] GenerateTileCollision2DFromLayer(
			this Map map, TileLayer layer,
			bool isTrigger = false, bool generateClosedPolygon = true, string tag = "Untagged",
			int physicsLayer = 0, PhysicsMaterial2D physicsMaterial = null, float zDepth = 1,
			bool simpleTileObjectCalculation = true,
			double clipperArcTolerance = 0.25, double clipperMiterLimit = 2.0,
			ClipperLib.JoinType clipperJoinType = ClipperLib.JoinType.jtRound,
			ClipperLib.EndType clipperEndType = ClipperLib.EndType.etClosedPolygon,
			float clipperDeltaOffset = 0)
        {
            if (layer == null)
                return null;

            if (layer.LayerTileCollisions == null)
            {
                layer.LayerTileCollisions = new GameObject(layer.Name + " Tile Collisions");
                Transform t = layer.LayerTileCollisions.transform;
                if (layer.BaseMap != null)
                {
                    t.parent = layer.BaseMap.MapGameObject.transform;
                }
                t.localPosition = Vector3.zero;
                t.localRotation = Quaternion.identity;
                t.localScale = Vector3.one;
                layer.LayerTileCollisions.isStatic = true;
            }
            layer.LayerTileCollisions.tag = tag;
            layer.LayerTileCollisions.layer = physicsLayer;

            List<GameObject> newSubCollider = new List<GameObject>();

            List<List<Vector2>> points = GenerateClipperPathPoints(layer, simpleTileObjectCalculation, clipperArcTolerance, clipperMiterLimit, clipperJoinType, clipperEndType, clipperDeltaOffset);

            for (int i = 0; i < points.Count; i++)
            {
                newSubCollider.Add(new GameObject("Tile Collisions " + layer.Name + "_" + i));
                newSubCollider[i].transform.parent = layer.LayerTileCollisions.transform;
                newSubCollider[i].transform.localPosition = new Vector3(0, 0, zDepth);
                newSubCollider[i].transform.localScale = Vector3.one;
                newSubCollider[i].transform.localRotation = Quaternion.identity;
                newSubCollider[i].tag = tag;
                newSubCollider[i].layer = physicsLayer;

                // Add the last point equals to the first to close the collider area
                // it's necessary only if the first point is diffent from the first one
                if (points[i][0].x != points[i][points[i].Count - 1].x || points[i][0].y != points[i][points[i].Count - 1].y)
                {
                    points[i].Add(points[i][0]);
                }

                Vector2[] pointsVec = points[i].ToArray();

                for (int j = 0; j < pointsVec.Length; j++)
                {
                    pointsVec[j] = map.TiledPositionToWorldPoint(pointsVec[j]);
                }

                if (generateClosedPolygon)
                {
                    PolygonCollider2D polyCollider = newSubCollider[i].AddComponent<PolygonCollider2D>();
                    polyCollider.isTrigger = isTrigger;
                    polyCollider.points = pointsVec;

                    if (physicsMaterial != null)
                        polyCollider.sharedMaterial = physicsMaterial;
                }
                else
                {
                    EdgeCollider2D edgeCollider = newSubCollider[i].AddComponent<EdgeCollider2D>();
                    edgeCollider.isTrigger = isTrigger;
                    edgeCollider.points = pointsVec;

                    if (physicsMaterial != null)
                        edgeCollider.sharedMaterial = physicsMaterial;
                }
            }
            return newSubCollider.ToArray();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj">MapObject which properties will be used to generate this collider</param>
        /// <param name="isTrigger">True for Trigger Collider, false otherwise</param>
        /// <param name="tag">Tag for the generated GameObject</param>
        /// <param name="physicsLayer">Physics Layer for the generated GameObject</param>
        /// <param name="physicsMaterial">PhysicsMaterial2D to be set to the collider</param>
        /// <param name="zDepth">Z Depth of the collider</param>
        /// <param name="createRigidbody">True to attach a Rigidbody to the created collider</param>
        /// <param name="rigidbodyIsKinematic">Sets if the attached rigidbody is kinematic or not</param>
        /// <returns>Generated Game Object containing the Collider</returns>
        public static GameObject GenerateBoxCollider2D(this Map map, MapObject obj, bool isTrigger = false, string tag = "Untagged", int physicsLayer = 0, PhysicsMaterial2D physicsMaterial = null, float zDepth = 0, bool createRigidbody = false, bool rigidbodyIsKinematic = true)
        {
            GameObject newCollider = new GameObject(obj.Name);
            newCollider.transform.parent = obj.ParentObjectLayer != null ? obj.ParentObjectLayer.LayerGameObject.transform : map.MapGameObject.transform;
            newCollider.transform.localPosition = map.TiledPositionToWorldPoint(obj.Bounds.x, obj.Bounds.y, zDepth);
            newCollider.transform.localScale = Vector3.one;
            newCollider.transform.localRotation = Quaternion.identity;
            newCollider.tag = tag;
            newCollider.layer = physicsLayer;

            AddBoxCollider2D(map, newCollider, obj, isTrigger, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);

            newCollider.isStatic = true;
            newCollider.SetActive(obj.Visible);

            return newCollider;
        }
        public static void AddPolylineCollider2D(Map map, GameObject gameObject, MapObject obj, bool isTrigger = false, PhysicsMaterial2D physicsMaterial = null, float zDepth = 0, bool createRigidbody = false, bool rigidbodyIsKinematic = true)
        {
            EdgeCollider2D edgeCollider = gameObject.AddComponent<EdgeCollider2D>();

            edgeCollider.isTrigger = isTrigger || obj.Type.Equals(Map.Object_Type_Trigger);

            Vector2[] points = obj.Points.ToArray();

            for (int i = 0; i < points.Length; i++)
            {
                points[i] = map.TiledPositionToWorldPoint(points[i].x, points[i].y);
                if (map.MapRenderParameter.Orientation == X_UniTMX.Orientation.Isometric)
                    points[i].x -= map.MapRenderParameter.Width / 2.0f;
            }

            edgeCollider.points = points;
            if (physicsMaterial != null)
                edgeCollider.sharedMaterial = physicsMaterial;

            if (createRigidbody)
            {
                Rigidbody2D r = gameObject.AddComponent<Rigidbody2D>();
                r.isKinematic = rigidbodyIsKinematic;
            }

            if (obj.Rotation != 0)
                gameObject.transform.localRotation = Quaternion.AngleAxis(obj.Rotation, Vector3.forward);

            ApplyCustomProperties(gameObject, obj);

            // Link this collider to the MapObject
            obj.LinkedGameObject = gameObject;
        }
 //====================================================================================================================================================
 //Unity defined functions
 // Use this for initialization
 void Start()
 {
     col = GetComponent<Collider2D> (); //get reference to the collider of the block
     sp = GetComponent<SpriteRenderer>();
     current = null; //initialize current material to null - meaning no material in effect
 }
        public static GameObject GenerateCollider2D(this Map map, MapObject obj, bool isTrigger = false, string tag = "Untagged", int physicsLayer = 0, PhysicsMaterial2D physicsMaterial = null, float zDepth = 0, bool createRigidbody = false, bool rigidbodyIsKinematic = true)
        {
            GameObject col = null;

            switch (obj.ObjectType)
            {
                case ObjectType.Box:
                    col = map.GenerateBoxCollider2D(obj, isTrigger, tag, physicsLayer, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);
                    break;
                case ObjectType.Ellipse:
                    col = map.GenerateEllipseCollider2D(obj, isTrigger, tag, physicsLayer, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);
                    break;
                case ObjectType.Polygon:
                    col = map.GeneratePolygonCollider2D(obj, isTrigger, tag, physicsLayer, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);
                    break;
                case ObjectType.Polyline:
                    col = map.GeneratePolylineCollider2D(obj, isTrigger, tag, physicsLayer, physicsMaterial, zDepth, createRigidbody, rigidbodyIsKinematic);
                    break;
            }

            return col;
        }