Last active
June 30, 2021 10:17
-
-
Save tsubaki/e7280fe3f02665089d6499669b7483d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor.Experimental.AssetImporters; | |
using System.IO; | |
using UnityEditor; | |
using UnityEngine.Assertions; | |
using System.Linq; | |
[ScriptedImporter(1, "cube")] | |
public class CubeImporter : ScriptedImporter | |
{ | |
public override void OnImportAsset(AssetImportContext ctx) | |
{ | |
var cube = AssetDatabase.LoadMainAssetAtPath(ctx.assetPath) as GameObject; | |
cube = GameObject.CreatePrimitive(PrimitiveType.Cube); | |
cube.transform.localScale = new Vector3(1, 1, 1); | |
var assets = AssetDatabase.LoadAllAssetsAtPath (ctx.assetPath); | |
var material = assets.FirstOrDefault (c => c.name == "Cube Material") as Material; | |
if (material == null) { | |
material = new Material(Shader.Find("Standard")); | |
material.name = "Cube Material"; | |
} | |
cube.GetComponent<MeshRenderer> ().material = material; | |
try | |
{ | |
var scale = JsonUtility.FromJson<Vector3>(File.ReadAllText(ctx.assetPath)); | |
cube.transform.localScale = scale; | |
}catch (System.Exception e) { | |
Debug.LogError (e.Message, cube); | |
} | |
ctx.AddObjectToAsset(material.name, material); | |
ctx.AddObjectToAsset("CUBE", cube); | |
ctx.SetMainObject(cube); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment