Files
AIGame/AIClient/AgentScripts/BuildCampAssets.cs
T

81 lines
5.9 KiB
C#

using System;
using System.IO;
using AIGame.EditorTools;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
public static class BuildCampAssets
{
const string World="Assets/Bundles/Scenes/Prototype";
const string Pictures="Assets/Bundles/UI/Texture/Prototype";
static Material Mat(string name) => AssetDatabase.LoadAssetAtPath<Material>(World+"/Materials/Proto"+name+".mat");
static Transform Part(Transform parent,string name,PrimitiveType type,Vector3 position,Vector3 scale,string material)
{
var go=GameObject.CreatePrimitive(type);go.name=name;go.layer=30;go.transform.SetParent(parent,false);
go.transform.localPosition=position;go.transform.localScale=scale;go.GetComponent<Renderer>().sharedMaterial=Mat(material);
UnityEngine.Object.DestroyImmediate(go.GetComponent<Collider>());return go.transform;
}
static void Prop(int id,Transform root)
{
if(id==0)
{
Part(root,"Foot",PrimitiveType.Cylinder,new Vector3(0,.06f,0),new Vector3(.7f,.06f,.7f),"Cream");
Part(root,"Pole",PrimitiveType.Cylinder,new Vector3(0,1,0),new Vector3(.09f,1,.09f),"Dark");
Part(root,"Finial",PrimitiveType.Sphere,new Vector3(0,2.03f,0),Vector3.one*.18f,"Yellow");
Part(root,"Flag",PrimitiveType.Cube,new Vector3(.43f,1.62f,0),new Vector3(.86f,.58f,.06f),"Orange");
Part(root,"Stripe",PrimitiveType.Cube,new Vector3(.43f,1.62f,-.04f),new Vector3(.7f,.12f,.025f),"Cream");
}
else if(id==1)
{
Part(root,"Pot",PrimitiveType.Cylinder,new Vector3(0,.27f,0),new Vector3(.8f,.27f,.8f),"Orange");
Part(root,"Rim",PrimitiveType.Cylinder,new Vector3(0,.53f,0),new Vector3(.9f,.055f,.9f),"Cream");
Part(root,"Stem",PrimitiveType.Cylinder,new Vector3(0,.9f,0),new Vector3(.09f,.4f,.09f),"Leaf");
for(int i=0;i<5;i++) {float a=i*Mathf.PI*2/5;Part(root,"Petal"+i,PrimitiveType.Sphere,new Vector3(Mathf.Sin(a)*.34f,1.37f+Mathf.Cos(a)*.34f,0),new Vector3(.45f,.45f,.2f),"Water");}
Part(root,"Heart",PrimitiveType.Sphere,new Vector3(0,1.37f,-.13f),Vector3.one*.36f,"Yellow");
Part(root,"Leaf",PrimitiveType.Sphere,new Vector3(.22f,.87f,0),new Vector3(.55f,.15f,.25f),"Leaf").localRotation=Quaternion.Euler(0,0,28);
}
else
{
Part(root,"Base",PrimitiveType.Cylinder,new Vector3(0,.08f,0),new Vector3(.65f,.08f,.65f),"Rock");
Part(root,"Post",PrimitiveType.Cylinder,new Vector3(0,.85f,0),new Vector3(.11f,.8f,.11f),"Dark");
Part(root,"Housing",PrimitiveType.Cube,new Vector3(0,1.72f,0),new Vector3(.6f,.7f,.45f),"Dark");
Part(root,"Glass",PrimitiveType.Cube,new Vector3(0,1.72f,-.05f),new Vector3(.46f,.55f,.44f),"Yellow");
Part(root,"Cap",PrimitiveType.Cube,new Vector3(0,2.12f,0),new Vector3(.78f,.12f,.6f),"Rock");
Part(root,"Top",PrimitiveType.Sphere,new Vector3(0,2.25f,0),Vector3.one*.18f,"Orange");
}
}
static void BuildProp(int id)
{
var scene=EditorSceneManager.NewPreviewScene();var previous=RenderTexture.active;
var rt=new RenderTexture(256,256,24,RenderTextureFormat.ARGB32);
try
{
var root=new GameObject("CampDecoration"+id);SceneManager.MoveGameObjectToScene(root,scene);root.layer=30;Prop(id,root.transform);
PrefabUtility.SaveAsPrefabAsset(root,World+"/CampDecoration"+id+".prefab");
var cameraGo=new GameObject("Camera");SceneManager.MoveGameObjectToScene(cameraGo,scene);var camera=cameraGo.AddComponent<Camera>();camera.scene=scene;
camera.clearFlags=CameraClearFlags.SolidColor;camera.backgroundColor=Color.clear;camera.cullingMask=1<<30;camera.orthographic=true;camera.orthographicSize=1.35f;camera.allowHDR=false;
camera.transform.position=new Vector3(1.3f,1.7f,-4);camera.transform.LookAt(new Vector3(id==0?.22f:0,1.05f,0));camera.targetTexture=rt;
var lightGo=new GameObject("Light");SceneManager.MoveGameObjectToScene(lightGo,scene);var light=lightGo.AddComponent<Light>();light.type=LightType.Directional;light.intensity=1.15f;light.cullingMask=1<<30;lightGo.transform.rotation=Quaternion.Euler(35,-35,0);
camera.Render();RenderTexture.active=rt;var texture=new Texture2D(256,256,TextureFormat.RGBA32,false);
try {texture.ReadPixels(new Rect(0,0,256,256),0,0);texture.Apply();File.WriteAllBytes(Pictures+"/proto_decor_"+id+".png",texture.EncodeToPNG());}
finally {UnityEngine.Object.DestroyImmediate(texture);}
}
finally {RenderTexture.active=previous;EditorSceneManager.ClosePreviewScene(scene);rt.Release();UnityEngine.Object.DestroyImmediate(rt);}
string path=Pictures+"/proto_decor_"+id+".png";AssetDatabase.ImportAsset(path);
var importer=(TextureImporter)AssetImporter.GetAtPath(path);importer.textureType=TextureImporterType.Sprite;importer.spriteImportMode=SpriteImportMode.Single;importer.mipmapEnabled=false;importer.alphaIsTransparency=true;importer.textureCompression=TextureImporterCompression.Uncompressed;
var settings=new TextureImporterSettings();importer.ReadTextureSettings(settings);settings.spriteMeshType=SpriteMeshType.FullRect;importer.SetTextureSettings(settings);importer.SaveAndReimport();
}
public static string Build()
{
if(EditorApplication.isPlaying) throw new Exception("Build camp in Edit Mode");
for(int i=0;i<3;i++) BuildProp(i);
string repo=Directory.GetParent(Application.dataPath).Parent.FullName;
foreach(string screen in new[]{"Camp","Build"})
if(string.IsNullOrEmpty(JsonUIPrefabBuilder.BuildFromJsonFile(Path.Combine(repo,"Doc/UIPrefabCreater/UI_Prototype"+screen+".json")))) throw new Exception("Camp UI build failed");
SetupUiBundleCollector.Setup();AssetDatabase.SaveAssets();
return "Created three authored decorations and transparent portraits; generated two camp UI prefabs from JSON.";
}
}