Attach custom data to LowLevelPhysics2D API objects
Attach values and physics masks to physics objects so you can fetch them later.
阅读时间1 分钟最后更新于 18 天前
To attach custom data to an object you create using the API, use the type.
LowLevelPhysics2DPhysicsUserDataYou can attach data to the following types:
WorldBodyShapeChainJoint
Follow these steps:
-
Create ainstance and populate it with your custom fields. For example:
PhysicsUserDataPhysicsUserData physicsUserData = new PhysicsUserData { customObject = this, customBool = true, customFloat = 123.4f, customInt = 567, customPhysicsMask = PhysicsMask.All }; -
Assign ainstance to the
PhysicsUserDataproperty of the physics object. For example, to assign the custom data to a shape:userDatamyShape.userData = physicsUserData; -
To fetch the data, for example when you detect a collision, get theproperty of the physics object. For example:
userDataDebug.Log(myShape.userData.intValue);