Anchor
INFO
It takes about 5 minutes to read this article
Use Anchor to solve the situation that the rotation and scaling based on the game object cannot meet the requirements in scene editing and object mounting due to the fixed anchor point of the object.
Anchor
Usually, anchors of objects in the asset library are fixed, and performing object transformation based on the object’s anchor often does not meet the requirements. Anchor provides the ability for users to customize the anchor position of an object, using the Anchor object itself as the object's anchor. Moreover, through the three offset ways of auto-centering, vertex adsorption and manual offset, the operation of Empty Anchor Point in editing state can be improved. When Empty Anchor Point exists as parent object in the project,the surrounding box generated by all child objects is displayed in the scene. You can find the Anchor in the "Gameplay Object" section of the "Asset library".
Create Anchor
Create by placing asset:
Anchor The object itself can exist as a GameObject
in the game scene. You can drag the Anchor from the asset library into the Main Viewport or Object Manager to automatically generate an Anchor object.
- Find Anchor in asset library
- Drag the object into the Main Viewport or Object Manager
- Find the corresponding Anchor Object in the Object Manager on the right and customize the anchor transformation and anchor offset.
TIP
Setting the anchor offset does not change the actual Transform value of the anchor object. However, the anchor offset affects the pivot point of the anchor object under the editor and acts on the Transform modification.
Custom Anchorage Offset
Transform: When performing transformation, use the anchor position as the pivot point to move, rotate, and scale.
Anchor Offset: When performing the transformation, use the position after anchor offset as the pivot point to move, rotate, and scale.
Use Anchor
TIP
The anchor offset function only works under the Editing Mode. In runtime state of the game, Anchor performs the transformation based on its actual value and only exists as an empty object.
Get Anchor Object
Anchor object under Object in Object Manager:
Use the asyncFind()
to get Anchor object by its GameObject ID:
- Select the anchor object and right-click on Copy GameObject ID to obtain its ID. Note the difference between GameObject ID and Asset ID.
- Add the following code to the script's onStart method: The code will asynchronously look for the object with the ID and receive it as Anchor object.
if(SystemUtil.isServer()) {
let anchor = await GameObject.asyncFindGameObjectById("3819014E") as GameObject;
console.log("anchor gameObjectId " + anchor.gameObjectId);
}
if(SystemUtil.isServer()) {
let anchor = await GameObject.asyncFindGameObjectById("3819014E") as GameObject;
console.log("anchor gameObjectId " + anchor.gameObjectId);
}
Get it by script mount:
- Mount the script below the Anchor object
- Add the following code to the script's onStart method: The code retrieves the script's mounted objects and receives them as Anchor object.
let anchor = this.gameObject as GameObject;
let anchor = this.gameObject as GameObject;