public static GameObject CreateCamera(CameraData cameraData) { CameraProjectionType camType = (CameraProjectionType)cameraData.type; Debug.LogFormat("Camera - Token:{0}, Name:{1}, Projection Type:{2}, VerticalFov:{3}, AspectRatio:{4}, HorizontalMag:{5}, NearClip:{6}, FarClip:{7}", cameraData.entityToken, cameraData.displayName, camType, cameraData.verticalFov, cameraData.aspectRatio, cameraData.horizontalMag, cameraData.nearClip, cameraData.farClip); // Get transformation data var transform = MarshalData.GetXFormFromUnmanagedArray(cameraData.xform); // Create a new game object GameObject cameraInst = new GameObject(cameraData.displayName); // Set transformation SceneTransmissionProtocolUtilities.UpdateObjectHierarchy(cameraInst, null, transform); // Add camera component Camera camera = cameraInst.AddComponent <Camera>(); SetCameraData(camera, cameraData); // Add token store PackageMapper.AddUniqueTokenStore(cameraInst, cameraData.entityToken); return(cameraInst); }
public static GameObject CreateMeshInstance(MeshInstanceData meshInstanceData) { Debug.LogFormat("Mesh Instance - Token:{0}, Name:{1} Parent:{2}, Mesh:{3}", meshInstanceData.entityToken, meshInstanceData.displayName, meshInstanceData.entityParentToken, meshInstanceData.originalMesh); var transform = MarshalData.GetXFormFromUnmanagedArray(meshInstanceData.xform); // Find mesh prefab Object meshPrefabObj = PackageMapper.GetObjectFromToken(meshInstanceData.originalMesh); // Create a new game object GameObject meshInst = Object.Instantiate(meshPrefabObj as GameObject); // Remove the "(Clone)" string from name that Unity automatically adds when instantiating // and apply the actual display name of the instance meshInst.name = meshInstanceData.displayName; // Find instance parent Object parentObj = PackageMapper.GetObjectFromToken(meshInstanceData.entityParentToken); SceneTransmissionProtocolUtilities.UpdateObjectHierarchy(meshInst, parentObj as GameObject, transform); // Add token store PackageMapper.AddUniqueTokenStore(meshInst, meshInstanceData.entityToken); return(meshInst); }
/// <summary> /// CreateNonSTPMaterialPrefab /// Create a prefab for materials not created by STP to store token information /// and connect the token to the material /// </summary> public static GameObject CreateNonSTPMaterialPrefab(Material material) { // Create material default STP folders if they don't exist PackageMapper.CreateAssetFolders(MaterialDefinitionHandler.assetPath); // Generate a unique material name string materialPath = Application.dataPath + "/" + MaterialDefinitionHandler.assetPath; string uniqueMaterialName; GenUniqueMaterialAssetName(materialPath, material.name, out uniqueMaterialName); // Create a GameObject prefab object and use it as a database item of STP used materials GameObject materialPrefab = new GameObject(uniqueMaterialName); // Add Foundry unique token storage component, store unique material token var tokenStorage = materialPrefab.AddComponent <FoundryUniqueToken>(); tokenStorage.uniqueToken = ExportUtilities.GenerateMaterialToken(material); // Add Foundry material storage component, store non STP material var materialStorage = materialPrefab.AddComponent <FoundryMaterialStore>(); materialStorage.material = material; // Create the prefab asset var localPrefabPath = System.String.Format("{0}/{1}/{2}", PackageMapper.rootPath, MaterialDefinitionHandler.assetPath, uniqueMaterialName); string prefabPath; PackageMapper.GenUniquePrefabAssetPath(localPrefabPath, out prefabPath); #if UNITY_2018_3_OR_NEWER Object prefabAsset = PrefabUtility.SaveAsPrefabAsset(materialPrefab, prefabPath); #else Object prefabAsset = PrefabUtility.CreatePrefab(prefabPath, materialPrefab); #endif // Add the prefab object to package mapper PackageMapper.AddUniqueTokenStore(prefabAsset as GameObject, tokenStorage.uniqueToken); PackageMapper.MapMaterialToPrefab(material, prefabAsset); AssetDatabase.SaveAssets(); // Remove game object that's used for creating a prefab from scene hierarchy Object.DestroyImmediate(materialPrefab); return(prefabAsset as GameObject); }
private static string GenerateMeshInstanceToken(GameObject obj) { // If it doesn't exist generate a new one string uniqueMeshInstanceToken; var scene = SceneManager.GetActiveScene(); uniqueMeshInstanceToken = "/"; uniqueMeshInstanceToken += (System.String.IsNullOrEmpty(scene.name)) ? "Untitled" : scene.name; uniqueMeshInstanceToken += "/instances/"; uniqueMeshInstanceToken += obj.name; uniqueMeshInstanceToken += "_inst"; if (PackageMapper.TokenExistsInCache(uniqueMeshInstanceToken)) { appendUniqueSuffix(ref uniqueMeshInstanceToken); } // Add unique token store to the GameObject and add the token to the mapper // so we can reuse and update the mesh instance with STP PackageMapper.AddUniqueTokenStore(obj, uniqueMeshInstanceToken); PackageMapper.MapTokenToObject(uniqueMeshInstanceToken, obj); return(uniqueMeshInstanceToken); }