In this chapter, we'll cover how to add ConjureKit entities to a shared session for each cube that the participants create.
Follow these steps to add an ^Entity^ to the session and instantiate a Primitive Cube that would appear in the same location in AR for all participants in the session:
1. Create a method, ^CreateCubeEntity^
, that adds an ^Entity^ to the session
public void CreateCubeEntity()
{
if (_conjureKit.GetState() != State.Calibrated)
return;
Vector3 position = arCamera.transform.position + arCamera.transform.forward * 0.5f;
Quaternion rotation = Quaternion.Euler(0, arCamera.transform.eulerAngles.y, 0);
Pose entityPos = new Pose(position, rotation);
_conjureKit.GetSession().AddEntity(
entityPos,
onComplete: entity => CreateCube(entity),
onError: error => Debug.Log(error));
}
Calling ^AddEntity^ adds a new ^Entity^ to the ^Session^ and invokes the ^onComplete^
callback, ^CreateCube^
, with the newly created entity.
_conjureKit.GetSession().AddEntity(
entityPos,
onComplete: entity => CreateCube(entity),
onError: error => Debug.Log(error));
7. We need something to visualize the Entities. Declare a public GameObject variable to reference a cube prefab. Implement the ^CreateCube^
method that instantiates a cube with a Pose.
[SerializeField] private GameObject cube;
private void CreateCube(Entity entity)
{
if (entity.Flag == EntityFlag.EntityFlagParticipantEntity) return;
var pose = _conjureKit.GetSession().GetEntityPose(entity);
Instantiate(cube, pose.position, pose.rotation);
}
We first check that the entity is not the special participant entity that is created automatically when a participant joins a session.
3. Declare a Button
variable to reference the button that will invoke CreateCubeEntity
method.
[SerializeField] private Button spawnButton;
We want this button to be interactable only when the session state is calibrated. Add a ^ToggleControlsState^
method and invoke it in ^_conjureKit.OnStateChanged^
callback.
_conjureKit.OnStateChanged += state =>
{
sessionState.text = state.ToString();
ToggleControlsState(state == State.Calibrated);
};
4. Create a primitive cube by selecting GameObject -> 3D Object -> Cube. Change the scale of the cube to ^0.1^
so it appears as a 10cm cube in AR. Drag and drop it into the Assets folder in the Project window to create a prefab and delete the cube from the scene. Drag the cube prefab to the field you declared in step 2.
5. Add a Button
to the scene, configure the on click callback to invoke ^CreateCubeEntity^
method. Drag the button game object to the field you declared in step 3.
If you run the project now you should see the cube in front of the camera.
申请 AUKI 代币补助金以启动您的项目,并直接与 Auki Labs 团队合作,将您的创意推向市场。成功申请者可获得价值高达 10 万美元的 AUKI 代币,以及 Auki Labs 团队提供的开发和营销支持。