Want to add an associated IndustrialStorageAdaptor to a StorageContainer, how should I use code to automatically add an associated IndustrialStorageAdaptor to the StorageContainer when the player places it?想要添加关联的IndustrialStorageAdaptor到StorageContainer,我应该如何使用代码来自动添加关联的IndustrialStorageAdaptor到StorageContainer时,播放器放置它?
How to add associated IndustrialStorageAdaptor to StorageContainer
After searching for information and trying, I used the following code to successfully add two IndustrialStorageAdaptors and a StashContainer to HitchTrough.在搜索信息并尝试之后,我使用以下代码成功地将两个IndustrialStorageAdaptors和一个StashContainer添加到HitchTrough。
But unfortunately, they were not oriented and positioned correctly, and my efforts to adjust them were always wrong, and the position changed each time I placed them, instead of being in the same position as the first one.但不幸的是,它们的方向和位置都不正确,我调整它们的努力总是错误的,每次放置它们的位置都在变化,而不是和第一个相同的位置。
Vector3 forward = Vector3.forward * 0.2f;
Vector3 up = Vector3.up * 0.2f;
Vector3 Position = forward + up;
Quaternion rotation = Quaternion.Euler(80, 0, 0);
IndustrialStorageAdaptor StorageLeft = AddStorageAdaptor(parentEntity, new Vector3() + Position + Vector3.left * 0.8f, rotation, true);
StashContainer Stash = AddStashContainer(parentEntity, new Vector3() + Position, rotation, true);
IndustrialStorageAdaptor StorageRight = AddStorageAdaptor(Stash, new Vector3() + Position + Vector3.right * 0.8f, rotation, true);
IndustrialStorageAdaptor AddStorageAdaptor(BaseEntity entity, Vector3 vector3, Quaternion rotation, bool SetOwnerID = false)
{
string StorageAdaptorPrefab = "assets/prefabs/deployable/playerioents/industrialadaptors/storageadaptor.deployed.prefab";
Socket_Base sock = PrefabAttribute.server.Find<Socket_Base>(entity.prefabID);
Vector3 socketPoint = entity.transform.TransformPoint(sock.worldPosition);
Vector3 StoragePosition = socketPoint + vector3;
Quaternion StorageRotation = new Quaternion()
{
x = entity.transform.rotation.x + rotation.x,
y = entity.transform.rotation.y + rotation.y,
z = entity.transform.rotation.z + rotation.z,
w = entity.transform.rotation.w + rotation.w,
};
IndustrialStorageAdaptor Storage = GameManager.server.CreateEntity(StorageAdaptorPrefab, StoragePosition, StorageRotation).GetComponent<IndustrialStorageAdaptor>();
if (entity.OwnerID.IsSteamId() && SetOwnerID)
{
Storage.OwnerID = entity.OwnerID;
}
Storage.Spawn();
Storage.SetParent(entity, true, true);
Storage.SendNetworkUpdate();
return Storage;
}
StashContainer AddStashContainer(BaseEntity entity, Vector3 vector3, Quaternion rotation, bool SetOwnerID = false)
{
string StashPrefab = "assets/prefabs/deployable/small stash/small_stash_deployed.prefab";
Socket_Base sock = PrefabAttribute.server.Find<Socket_Base>(entity.prefabID);
Vector3 socketPoint = entity.transform.TransformPoint(sock.worldPosition);
Vector3 StashPosition = socketPoint + vector3;
Quaternion StashRotation = new Quaternion()
{
x = entity.transform.rotation.x + rotation.x,
y = entity.transform.rotation.y + rotation.y,
z = entity.transform.rotation.z + rotation.z,
w = entity.transform.rotation.w + rotation.w,
};
StashContainer Stash = GameManager.server.CreateEntity(StashPrefab, StashPosition, StashRotation).GetComponent<StashContainer>();
if (entity.OwnerID.IsSteamId() && SetOwnerID)
{
Stash.OwnerID = entity.OwnerID;
}
Stash.Spawn();
Stash.SetParent(entity, true, true);
Stash.SendNetworkUpdate();
return Stash;
}
The final usage effect is as follows:最终使用效果如下:最终使用效果如下:最终使用效果如下:最终使用效果如下: