public override void OnPostImport(string assetPath) { XmlDocument doc = new XmlDocument(); doc.Load(assetPath); XmlNode root = doc.DocumentElement; string prefabPath = root.SelectSingleNode("Path").InnerText; string fileName = Path.GetFileNameWithoutExtension(assetPath); string fullPath = Path.Combine(prefabPath, fileName + ".prefab"); // Load the prefab asset GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject; if (prefab == null) { SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath); return; } GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject; Camera camera = prefabInstance.GetOrAddComponent <Camera>(); string projection = root.SelectSingleNode("Projection").InnerText; camera.orthographic = (projection != "PERSP"); float fov = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Fov").InnerText); camera.fieldOfView = (Mathf.Atan(Mathf.Tan(Mathf.Deg2Rad * fov / 2.0f) / camera.aspect) * 2.0f) * Mathf.Rad2Deg; float near = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Near").InnerText); camera.nearClipPlane = near; float far = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Far").InnerText); camera.farClipPlane = far; float size = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Size").InnerText); camera.orthographicSize = size * 0.28f; // Save prefab asset PrefabUtility.RecordPrefabInstancePropertyModifications(camera); PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction); // Clean up GameObject.DestroyImmediate(prefabInstance); // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor) AssetDatabase.ImportAsset(fullPath); }
public bool SetFloat(XmlNode channels, string valueName) { XmlNode nodeXml = channels.SelectSingleNode(valueName); if (nodeXml != null) { float value = SmallParserUtils.ParseFloatXml(nodeXml.InnerText); _material.SetFloat(valueName, value); return(true); } return(false); }
public void SetTransparency(XmlNode channels, string propName) { XmlNode transparentXml = channels.SelectSingleNode("_Transparent"); if (transparentXml != null) { string value = transparentXml.InnerText; float mode = 0.0f; float threshold = 0.5f; if (value == "OPAQUE") { mode = 0.0f; } else if (value == "CLIP") { mode = 1.0f; XmlNode clipThresholdXml = channels.SelectSingleNode("_Cutoff"); if (clipThresholdXml != null) { threshold = SmallParserUtils.ParseFloatXml(clipThresholdXml.InnerText); } } else if (value == "BLEND") { mode = 2.0f; } // _Mode is for standard shaders _material.SetFloat("_Mode", mode); // Surface is for URP // Set surface to 1 if transparent _material.SetFloat("_Surface", mode > 0.0f ? 1.0f : 0.0f); _material.SetFloat("_AlphaClip", mode == 1.0f ? 1.0f : 0.0f); _material.SetFloat("_Cutoff", threshold); } }
public void SetLightmap(XmlNode channels, string propName) { // Tile and lightmap data XmlNode tileMaxXml = channels.SelectSingleNode("_TileMax"); if (tileMaxXml != null) { float tileMax = SmallParserUtils.ParseFloatXml(tileMaxXml.InnerText); float tileScale = 1.0f / tileMax; _material.SetFloat("_TileScale", tileScale); float tileX = SmallParserUtils.ParseFloatXml(channels.SelectSingleNode("_TileX").InnerText); float tileY = SmallParserUtils.ParseFloatXml(channels.SelectSingleNode("_TileY").InnerText); _material.SetVector("_Offset", new Vector4(tileX * tileScale, tileY * tileScale)); // Lightmap XmlNode lightmapXml = channels.SelectSingleNode("_Lightmap"); if (lightmapXml != null) { Texture texture = SmallParserUtils.ParseTextureXml(lightmapXml.InnerText); _material.SetTexture("_Lightmap", texture); } } }
public override void OnPostImport(string assetPath) { XmlDocument doc = new XmlDocument(); doc.Load(assetPath); XmlNode root = doc.DocumentElement; string prefabPath = root.SelectSingleNode("Path").InnerText; string fileName = Path.GetFileNameWithoutExtension(assetPath); string fullPath = Path.Combine(prefabPath, fileName + ".prefab"); // Load the prefab asset GameObject prefab = AssetDatabase.LoadMainAssetAtPath(fullPath) as GameObject; if (prefab == null) { SmallLogger.LogWarning(SmallLogger.LogType.PostImport, "There is no prefab at path " + fullPath); return; } GameObject prefabInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject; Light light = prefabInstance.GetOrAddComponent <Light>(); // Light type string type = root.SelectSingleNode("Type").InnerText; if (type == "POINT") { light.type = LightType.Point; light.range = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Radius").InnerText); } else if (type == "SPOT") { light.type = LightType.Spot; light.spotAngle = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("SpotSize").InnerText); light.range = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Radius").InnerText); } else if (type == "SUN") { light.type = LightType.Directional; } else if (type == "AREA") { string shape = root.SelectSingleNode("Shape").InnerText; if (shape == "RECTANGLE") { light.type = LightType.Rectangle; float width = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Width").InnerText); float height = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Height").InnerText); light.areaSize = new Vector2(width, height); } else if (shape == "DISC") { light.type = LightType.Disc; float radius = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Radius").InnerText); light.areaSize = new Vector2(radius, radius); } } // Light color light.color = SmallParserUtils.ParseColorXml(root.SelectSingleNode("Color").InnerText); light.intensity = SmallParserUtils.ParseFloatXml(root.SelectSingleNode("Power").InnerText) / 100.0f; // Save prefab asset PrefabUtility.RecordPrefabInstancePropertyModifications(light); PrefabUtility.ApplyPrefabInstance(prefabInstance, InteractionMode.AutomatedAction); // Clean up GameObject.DestroyImmediate(prefabInstance); // Force Unity to update the asset, without this we have to manually reload unity (by losing and gaining focus on the editor) AssetDatabase.ImportAsset(fullPath); }