Advertisement
Guest User

Create New CGINC file (Unity Editor)

a guest
Dec 18th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6.  
  7. public static class CgIncUtilities
  8. {
  9.  
  10.     [MenuItem("Assets/Create/Shader/CgInc")]
  11.     public static void Create ()
  12.     {
  13.         string path = GetTargetPath(Selection.activeObject);
  14.         string name = "NewCginc";
  15.         string ext = ".cginc";
  16.         string fullPath = AssetDatabase.GenerateUniqueAssetPath(path + name + ext);
  17.         string content = "" +
  18.             "#ifndef NEW_CGINC_INCLUDED \n" +
  19.             "#define NEW_CGINC_INCLUDED \n" +
  20.             " \n" + "// TODO : " + "Amazing CG \n" + " \n" +
  21.             "#endif";
  22.  
  23.         File.WriteAllText(fullPath, content);
  24.         AssetDatabase.ImportAsset(fullPath, ImportAssetOptions.ForceUpdate);
  25.  
  26.         EditorUtility.FocusProjectWindow();
  27.         Selection.activeObject = AssetDatabase.LoadAssetAtPath<TextAsset>(fullPath);
  28.     }
  29.  
  30.     static string GetTargetPath(Object obj)
  31.     {
  32.         string targetPath = "Assets/";
  33.         if (obj == null) return targetPath;
  34.  
  35.         string objPath = AssetDatabase.GetAssetPath(obj);
  36.         if (string.IsNullOrEmpty(objPath)) return targetPath;
  37.  
  38.         if (AssetDatabase.IsValidFolder(objPath)) targetPath = objPath + "/";
  39.         else
  40.         {
  41.             targetPath = "";
  42.             string[] splitPath = objPath.Split(new string[1]{"/"}, System.StringSplitOptions.None);
  43.             for (int i=0; i<splitPath.Length-1; i++) targetPath += splitPath[i] + "/";
  44.         }
  45.  
  46.         return targetPath;
  47.     }
  48.    
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement