Advertisement
Guest User

Editorfiller_TowerUVs.cs

a guest
Aug 23rd, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7.  
  8. public class Editorfiller_TowerUVs : EditorWindow {
  9.  
  10.     public GameObject Towermodel;
  11.     private Vector2 scrollview1 = Vector2.zero;
  12.     private int ain,bin,cin,din,ein,fin = 0;
  13.     private string dateiname = "";
  14.  
  15.     private GUIStyle gsAlterQuest = new GUIStyle();
  16.     [System.Serializable]
  17.     public class UVs{
  18.         public float toplx;
  19.         public float toprx;
  20.         public float lowlx;
  21.         public float lowrx;
  22.        
  23.         public float toply;
  24.         public float topry;
  25.         public float lowly;
  26.         public float lowry;
  27.  
  28.         public int toplid;
  29.         public int toprid;
  30.         public int lowlid;
  31.         public int lowrid;
  32.  
  33.         public UVs(){
  34.             toplx = 0.0f;
  35.             toprx = 0.0f;
  36.             lowlx = 0.0f;
  37.             lowrx = 0.0f;
  38.             toply = 0.0f;
  39.             topry = 0.0f;
  40.             lowly = 0.0f;
  41.             lowry = 0.0f;
  42.             toplid = 0;
  43.             toprid = 0;
  44.             lowlid = 0;
  45.             lowrid = 0;
  46.         }
  47.     }
  48.  
  49.     public List<UVs> UVSet = new List<UVs>();
  50.    
  51.     [MenuItem ("BDC Editors/UV Getter")]
  52.     public static void  ShowWindow () {
  53.         EditorWindow.GetWindow(typeof(Editorfiller_TowerUVs));
  54.     }
  55.  
  56.     // Use this for initialization
  57.     void OnGUI(){
  58.         gsAlterQuest.normal.background = MakeTex(1, 1, new Color(1.0f, 1.0f, 1.0f, 0.1f));
  59.         Towermodel = EditorGUILayout.ObjectField(Towermodel, typeof(GameObject), true) as GameObject;
  60.         GUILayout.BeginHorizontal();
  61.             GUILayout.Label("Dateiname (ohne Endung): ");
  62.             dateiname = GUILayout.TextField(dateiname);
  63.         GUILayout.EndHorizontal();
  64.         GUILayout.BeginHorizontal();
  65.             GUILayout.Label("Vertice pro Reihe: ");
  66.             ein = EditorGUILayout.IntField(ein);
  67.         GUILayout.EndHorizontal();
  68.  
  69.         GUILayout.Label("Listengröße: " + UVSet.Count);
  70.         scrollview1 = GUILayout.BeginScrollView(scrollview1);
  71.  
  72.         for(ain=0; ain<UVSet.Count; ain++){
  73.             GUILayout.BeginHorizontal(gsAlterQuest);
  74.                 GUILayout.Label("Quad ID: " + ain);
  75.                 GUILayout.BeginVertical();
  76.                     GUILayout.Label("UV TopL: " + UVSet[ain].toplid + " >>  " + UVSet[ain].toplx + "/" + UVSet[ain].toply);
  77.                     GUILayout.Label("UV LowL: " + UVSet[ain].lowlid + " >>  " + UVSet[ain].lowlx + "/" + UVSet[ain].lowly);
  78.                 GUILayout.EndVertical();
  79.                 GUILayout.BeginVertical();
  80.                     GUILayout.Label("UV TopR: " + UVSet[ain].toprid + " >>  " + UVSet[ain].toprx + "/" + UVSet[ain].topry);
  81.                     GUILayout.Label("UV LowR: " + UVSet[ain].lowrid + " >>  " + UVSet[ain].lowrx + "/" + UVSet[ain].lowry);
  82.                 GUILayout.EndVertical();
  83.             GUILayout.EndHorizontal();
  84.             GUILayout.Space(4);
  85.         }
  86.  
  87.         GUILayout.EndScrollView();
  88.  
  89.  
  90.         if(GUILayout.Button("Saving")){
  91.             Saveing(dateiname + ".dat");
  92.         }
  93.         if(GUILayout.Button("Loading")){
  94.             UVSet = Loading(dateiname + ".dat");
  95.         }
  96.         if(GUILayout.Button("Clearing")){
  97.             UVSet = new List<UVs>();
  98.             cin = 0;
  99.             din = 0;
  100.             fin = 1;
  101.             for(bin=0; bin<Towermodel.GetComponent<MeshFilter>().sharedMesh.vertexCount/4; bin++){
  102.                 UVSet.Add(new UVs());
  103.                 if(UVSet[bin] != null){
  104.                     UVSet[bin].toplid = cin;
  105.                     din = cin+ein;
  106.                     cin += 1;
  107.                     UVSet[bin].toprid = cin;
  108.                     UVSet[bin].lowlid = din;
  109.                     UVSet[bin].lowrid = din+1;
  110.                     fin += 1;
  111.                     if(fin == ein){
  112.                         cin += 1;
  113.                         fin = 1;
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.  
  119.  
  120.         if(GUILayout.Button("Set Null Room")){
  121.             for(bin=0; bin<UVSet.Count; bin++){
  122.                 if(UVSet[bin] != null){
  123.                     UVSet[bin].toplx = 0.0f;
  124.                     UVSet[bin].toply = 1.0f;
  125.  
  126.                     UVSet[bin].toprx = 0.0625f;
  127.                     UVSet[bin].topry = 1.0f;
  128.  
  129.                     UVSet[bin].lowlx = 0.0f;
  130.                     UVSet[bin].lowly = 1.0f - 0.0390625f;
  131.  
  132.                     UVSet[bin].lowrx = 0.0625f;
  133.                     UVSet[bin].lowry = 1.0f - 0.0390625f;
  134.                 }
  135.                 Towermodel.GetComponent<MeshFilter>().sharedMesh.uv[UVSet[bin].toplid].x = UVSet[bin].toplx;
  136.                 Towermodel.GetComponent<MeshFilter>().sharedMesh.uv[UVSet[bin].toplid].y = UVSet[bin].toply;
  137.                
  138.                 Towermodel.GetComponent<MeshFilter>().sharedMesh.uv[UVSet[bin].toprid].x = UVSet[bin].toprx;
  139.                 Towermodel.GetComponent<MeshFilter>().sharedMesh.uv[UVSet[bin].toprid].y = UVSet[bin].topry;
  140.                
  141.                 Towermodel.GetComponent<MeshFilter>().sharedMesh.uv[UVSet[bin].lowlid].x = UVSet[bin].lowlx;
  142.                 Towermodel.GetComponent<MeshFilter>().sharedMesh.uv[UVSet[bin].lowlid].y = UVSet[bin].lowly;
  143.                
  144.                 Towermodel.GetComponent<MeshFilter>().mesh.uv[UVSet[bin].lowrid].x = UVSet[bin].lowrx;
  145.                 Towermodel.GetComponent<MeshFilter>().sharedMesh.uv[UVSet[bin].lowrid].y = UVSet[bin].lowry;
  146.                 Debug.Log("Changed Room: " + bin);
  147.             }
  148.         }
  149.     }
  150.  
  151.     private Texture2D MakeTex(int width, int height, Color col)
  152.     {
  153.         Color[] pix = new Color[width*height];
  154.        
  155.         for(int i = 0; i < pix.Length; i++)
  156.             pix[i] = col;
  157.        
  158.         Texture2D result = new Texture2D(width, height);
  159.         result.SetPixels(pix);
  160.         result.Apply();
  161.        
  162.         return result;
  163.     }
  164.  
  165.     private void Saveing(string filename){
  166.         BinaryFormatter formatter = new BinaryFormatter();
  167.         using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
  168.         {
  169.             formatter.Serialize(fs, UVSet);
  170.             fs.Close();
  171.         }
  172.         Debug.LogError("UVs GESPEICHERT");
  173.     }
  174.    
  175.     private List<UVs> Loading(string filename)
  176.     {
  177.         BinaryFormatter formatter = new BinaryFormatter();
  178.         using (FileStream fs = new FileStream(filename, FileMode.Open))
  179.         {
  180.             List<UVs> result = formatter.Deserialize(fs) as List<UVs>;
  181.             fs.Close();
  182.             return result;
  183.         }
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement