public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx) { var prefax = Path.GetExtension(ctx.assetPath).Substring(1); var text = File.ReadAllText(ctx.assetPath); var asset = new TextAsset(text); ctx.AddObjectToAsset("main obj", asset, LoadIconTexture(prefax)); ctx.SetMainObject(asset); }
public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx) { var str = File.ReadAllText(ctx.assetPath); var textAsset = new TextAsset(str); ctx.AddObjectToAsset("text", textAsset); ctx.SetMainObject(textAsset); }
public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx) { string[] lines = File.ReadAllLines(ctx.assetPath); if (lines.Length > 0 && lines[0] == "WIREMESH") { Mesh m = new Mesh(); if (lines.Length > 1) { List <Vector3> vertices = new List <Vector3>(); int[] indices = new int[(lines.Length - 1) * 2]; for (int i = 1; i < lines.Length; i++) { string[] values = lines[i].Split(separators); if (values.Length != 6) { throw new System.IO.InvalidDataException("Invalid wire data length"); } float[] linedata = new float[6]; for (int j = 0; j < 6; j++) { linedata[j] = float.Parse(values[j], System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture); } vertices.Add(new Vector3(linedata[0], linedata[1], linedata[2])); vertices.Add(new Vector3(linedata[3], linedata[4], linedata[5])); indices[(i - 1) * 2] = (i - 1) * 2; indices[(i - 1) * 2 + 1] = (i - 1) * 2 + 1; } m.SetVertices(vertices); m.SetIndices(indices, MeshTopology.Lines, 0, true); } else { Debug.LogWarning("Empty Mesh"); } m.name = Path.GetFileNameWithoutExtension(ctx.assetPath); ctx.AddObjectToAsset("Mesh", m); ctx.SetMainObject(m); } else { throw new System.IO.InvalidDataException("Invalid File!"); } }
public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx) { BakedPointCloud data = ImportAsBakedPointCloud(ctx.assetPath); if (data != null) { ctx.AddObjectToAsset("container", data); ctx.AddObjectToAsset("position", data.PositionMap); ctx.AddObjectToAsset("color", data.ColorMap); ctx.SetMainObject(data); } }
public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx) { List <Object> objectList = new List <Object>(); Debug.Log("MAIN OBJECT " + ctx.mainObject); ctx.GetObjects(objectList); Debug.Log(" OBJECTS " + objectList.Count); var textGraph = File.ReadAllText(ctx.assetPath, Encoding.UTF8); LogicGraphObject loadedGraphObject = AssetDatabase.LoadAssetAtPath <LogicGraphObject>(ctx.assetPath); if (loadedGraphObject == null) { Debug.Log("Generating new"); var graph = JsonUtility.FromJson <LogicGraphData>(textGraph); LogicGraphObject logicGraphObject = ScriptableObject.CreateInstance <LogicGraphObject>(); logicGraphObject.Initialize(graph); ctx.AddObjectToAsset("MainAsset", logicGraphObject); ctx.SetMainObject(logicGraphObject); } else { Debug.Log("Updating Old"); JsonUtility.FromJsonOverwrite(textGraph, loadedGraphObject.GraphData); ctx.AddObjectToAsset("MainAsset", loadedGraphObject); ctx.SetMainObject(loadedGraphObject); } Debug.Log(loadedGraphObject); // AssetDatabase.SaveAssets(); // EditorSceneManager.SaveOpenScenes(); Debug.Log("Set Asset"); // AssetDatabase.Refresh(); }
private void ImportCompiledYarn(UnityEditor.AssetImporters.AssetImportContext ctx) { var bytes = File.ReadAllBytes(ctx.assetPath); try { // Validate that this can be parsed as a Program protobuf var _ = Program.Parser.ParseFrom(bytes); } catch (Google.Protobuf.InvalidProtocolBufferException) { ctx.LogImportError("Invalid compiled yarn file. Please re-compile the source code."); return; } isSuccesfullyCompiled = true; // Create a container for storing the bytes var programContainer = ScriptableObject.CreateInstance <YarnProgram>(); programContainer.compiledProgram = bytes; // Add this container to the imported asset; it will be // what the user interacts with in Unity ctx.AddObjectToAsset("Program", programContainer); ctx.SetMainObject(programContainer); }
public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx) { // NOTE: repere bas gauche, Y up. int ret = OIIOAPI.oiio_open_image(assetPath); if (ret == 0) { Debug.Log("could not open " + assetPath); return; } int width = -1; int height = -1; int nchannels = -1; OIIOAPI.BASETYPE format = OIIOAPI.BASETYPE.NONE; ret = OIIOAPI.oiio_get_image_info(ref width, ref height, ref nchannels, ref format); if (ret == 0) { Debug.Log("Could not get width/height of " + assetPath); return; } imageDimensions.Set(width, height); TextureFormat textureFormat = Format2Format(format, nchannels); var image = new Texture2D(width, height, textureFormat, false, true); // with mips, linear int do_rgb_to_rgba = 0; if ((format == OIIOAPI.BASETYPE.FLOAT && nchannels == 3) || (format == OIIOAPI.BASETYPE.HALF && nchannels == 3)) { do_rgb_to_rgba = 1; } //Color[] pixels = image.GetPixels(); var pixels = image.GetRawTextureData(); GCHandle handle = GCHandle.Alloc(pixels, GCHandleType.Pinned); ret = OIIOAPI.oiio_fill_image_data(handle.AddrOfPinnedObject(), do_rgb_to_rgba); if (ret == 1) { image.LoadRawTextureData(pixels); //image.SetPixels(pixels); image.Apply(); } else { Debug.Log("Could not fill texture data of " + assetPath); return; } #if UNITY_2017_3_OR_NEWER var filename = Path.GetFileNameWithoutExtension(assetPath); ctx.AddObjectToAsset(filename, image); ctx.SetMainObject(image); #else ctx.SetMainObject(image); #endif }
private void ImportYarn(UnityEditor.AssetImporters.AssetImportContext ctx) { var sourceText = File.ReadAllText(ctx.assetPath); string fileName = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath); try { // Compile the source code into a compiled Yarn program (or // generate a parse error) compilationStatus = Compiler.CompileString(sourceText, fileName, out var compiledProgram, out var stringTable); // Create a container for storing the bytes var programContainer = ScriptableObject.CreateInstance <YarnProgram>(); using (var memoryStream = new MemoryStream()) using (var outputStream = new Google.Protobuf.CodedOutputStream(memoryStream)) { // Serialize the compiled program to memory compiledProgram.WriteTo(outputStream); outputStream.Flush(); byte[] compiledBytes = memoryStream.ToArray(); programContainer.compiledProgram = compiledBytes; // Add this container to the imported asset; it will be // what the user interacts with in Unity ctx.AddObjectToAsset("Program", programContainer, YarnEditorUtility.GetYarnDocumentIconTexture()); ctx.SetMainObject(programContainer); isSuccesfullyCompiled = true; // var outPath = Path.ChangeExtension(ctx.assetPath, ".yarnc"); // File.WriteAllBytes(outPath, compiledBytes); } if (stringTable.Count > 0) { using (var memoryStream = new MemoryStream()) using (var textWriter = new StreamWriter(memoryStream)) { // Generate the localised .csv file // Use the invariant culture when writing the CSV var configuration = new CsvHelper.Configuration.Configuration( System.Globalization.CultureInfo.InvariantCulture ); var csv = new CsvHelper.CsvWriter( textWriter, // write into this stream configuration // use this configuration ); var lines = stringTable.Select(x => new { id = x.Key, text = x.Value.text, file = x.Value.fileName, node = x.Value.nodeName, lineNumber = x.Value.lineNumber }); csv.WriteRecords(lines); textWriter.Flush(); memoryStream.Position = 0; using (var reader = new StreamReader(memoryStream)) { var textAsset = new TextAsset(reader.ReadToEnd()); textAsset.name = $"{fileName} ({baseLanguageID})"; ctx.AddObjectToAsset("Strings", textAsset); programContainer.baseLocalisationStringTable = textAsset; baseLanguage = textAsset; programContainer.localizations = localizations; } stringIDs = lines.Select(l => l.id).ToArray(); } } } catch (Yarn.Compiler.ParseException e) { isSuccesfullyCompiled = false; compilationErrorMessage = e.Message; ctx.LogImportError(e.Message); return; } }
public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx) { try { PCache pcache = PCache.FromFile(ctx.assetPath); PointCacheAsset cache = ScriptableObject.CreateInstance <PointCacheAsset>(); cache.name = "PointCache"; ctx.AddObjectToAsset("PointCache", cache); ctx.SetMainObject(cache); List <InProperty> inProperties = new List <InProperty>(); Dictionary <string, OutProperty> outProperties = new Dictionary <string, OutProperty>(); Dictionary <OutProperty, Texture2D> surfaces = new Dictionary <OutProperty, Texture2D>(); foreach (var prop in pcache.properties) { OutProperty p_out; if (outProperties.ContainsKey(prop.ComponentName)) { p_out = outProperties[prop.ComponentName]; p_out.Size = Math.Max(p_out.Size, prop.ComponentIndex + 1); } else { p_out = new OutProperty(prop.Type, prop.ComponentName, prop.ComponentIndex + 1); outProperties.Add(prop.ComponentName, p_out); } inProperties.Add(new InProperty(prop.Type, prop.Name, prop.ComponentIndex, p_out)); } int width, height; FindBestSize(pcache.elementCount, out width, out height); // Output Surface Creation foreach (var kvp in outProperties) { TextureFormat surfaceFormat = TextureFormat.Alpha8; switch (kvp.Value.PropertyType) { case "byte": if (kvp.Value.Size == 1) { surfaceFormat = TextureFormat.Alpha8; } else { surfaceFormat = TextureFormat.RGBA32; } break; case "float": if (kvp.Value.Size == 1) { surfaceFormat = TextureFormat.RHalf; } else { surfaceFormat = TextureFormat.RGBAHalf; } break; default: throw new NotImplementedException("Types other than byte/float are not supported yet"); } Texture2D surface = new Texture2D(width, height, surfaceFormat, false); surface.name = kvp.Key; surfaces.Add(kvp.Value, surface); } cache.PointCount = pcache.elementCount; cache.surfaces = new Texture2D[surfaces.Count]; Dictionary <OutProperty, Color> outValues = new Dictionary <OutProperty, Color>(); foreach (var kvp in outProperties) { outValues.Add(kvp.Value, new Color()); } for (int i = 0; i < pcache.elementCount; i++) { int idx = 0; foreach (var prop in inProperties) { float val = 0.0f; switch (prop.PropertyType) { case "byte": val = Mathf.Clamp01(((int)pcache.buckets[idx][i]) / 256.0f); break; case "float": val = ((float)pcache.buckets[idx][i]); break; default: throw new NotImplementedException("Types other than byte/float are not supported yet"); } SetPropValue(prop.Index, outValues, prop.OutProperty, val); idx++; } foreach (var kvp in outProperties) { surfaces[kvp.Value].SetPixel(i % width, i / width, outValues[kvp.Value]); } } int k = 0; foreach (var kvp in surfaces) { kvp.Value.Apply(); kvp.Value.hideFlags = HideFlags.HideInHierarchy; ctx.AddObjectToAsset(kvp.Key.Name, kvp.Value); cache.surfaces[k] = kvp.Value; k++; } } catch (System.Exception e) { Debug.LogException(e); } }
public override void OnImportAsset(UnityEditor.AssetImporters.AssetImportContext ctx) { byte[] bytes = File.ReadAllBytes(ctx.assetPath); int width = 1, height = 1, depth = 1; try { int channels = 0; string fourcc = Encoding.UTF8.GetString(SubArray <byte>(bytes, 0, 4)); if (fourcc != "VF_F" && fourcc != "VF_V") { throw new Exception("Invalid VF File Header. Need VF_F or VF_V, found :" + fourcc); } else { if (fourcc == "VF_F") { channels = 1; } if (fourcc == "VF_V") { channels = 3; } } TextureFormat outFormat = TextureFormat.Alpha8; switch (m_OutputFormat) { case VectorFieldOutputFormat.Byte: outFormat = channels == 3 ? TextureFormat.RGBA32 : TextureFormat.Alpha8; break; case VectorFieldOutputFormat.Half: outFormat = channels == 3 ? TextureFormat.RGBAHalf : TextureFormat.RHalf; break; case VectorFieldOutputFormat.Float: outFormat = channels == 3 ? TextureFormat.RGBAFloat : TextureFormat.RFloat; break; } if (bytes.Length < 10) { throw new Exception("Malformed VF File, invalid header (less than 10 bytes)"); } width = BitConverter.ToUInt16(bytes, 4); height = BitConverter.ToUInt16(bytes, 6); depth = BitConverter.ToUInt16(bytes, 8); int requiredLength = 10 + (4 * channels * (width * height * depth)); if (bytes.Length != requiredLength) { throw new Exception("Malformed VF File, invalid length (expected :" + requiredLength + ", found :" + bytes.Length + ")"); } Texture3D texture = new Texture3D(width, height, depth, outFormat, m_GenerateMipMaps); texture.wrapMode = m_WrapMode; texture.filterMode = m_FilterMode; texture.anisoLevel = m_AnisoLevel; int count = width * height * depth; Color[] colors = new Color[count]; for (int i = 0; i < count; i++) { Color c; if (channels == 1) { float x = BitConverter.ToSingle(bytes, 10 + (i * 4 * channels)); c = new Color(x, 0, 0); } else { float x = BitConverter.ToSingle(bytes, 10 + (i * 4 * channels)); float y = BitConverter.ToSingle(bytes, 14 + (i * 4 * channels)); float z = BitConverter.ToSingle(bytes, 18 + (i * 4 * channels)); c = new Color(x, y, z); } colors[i] = c; } texture.SetPixels(colors); texture.Apply(true, true); ctx.AddObjectToAsset("VectorField", texture); ctx.SetMainObject(texture); } catch (System.Exception e) { Debug.LogException(e); } }