通用脚本 CommonWindows
public class CommonWindows
{
public const string Tools = "Tools" ;
public static T ReadDate < T> ( ) where T : BaseDate , new ( )
{
string path = typeof ( T ) . Name;
T date = null ;
var dataPath = System. IO. Path. GetFullPath ( "." ) ;
dataPath = dataPath. Replace ( "\\" , "/" ) ;
dataPath += "/Library/Tools/Tools_" + path + ".dat" ;
if ( File. Exists ( dataPath) )
{
BinaryFormatter bf = new BinaryFormatter ( ) ;
FileStream file = File. OpenRead ( dataPath) ;
try
{
date = bf. Deserialize ( file) as T ;
file. Close ( ) ;
}
catch
{
file. Close ( ) ;
Debug. LogError ( "Read dat error on path :" + dataPath) ;
File. Delete ( dataPath) ;
}
}
if ( date == null )
{
date = new T ( ) ;
date. Init ( ) ;
}
return date;
}
public static void SaveDate < T> ( T date) where T : BaseDate , new ( )
{
string path = typeof ( T ) . Name;
var dataPath = System. IO. Path. GetFullPath ( "." ) ;
dataPath = dataPath. Replace ( "\\" , "/" ) ;
dataPath += "/Library/Tools/Tools_" + path + ".dat" ;
if ( ! File. Exists ( dataPath) )
{
if ( ! Directory. Exists ( Path. GetDirectoryName ( dataPath) ) )
{
Directory. CreateDirectory ( Path. GetDirectoryName ( dataPath) ) ;
}
}
BinaryFormatter bf = new BinaryFormatter ( ) ;
FileStream file = File. OpenWrite ( dataPath) ;
try
{
bf. Serialize ( file, date) ;
file. Close ( ) ;
}
catch
{
file. Close ( ) ;
Debug. LogError ( "Save dat error on path :" + dataPath) ;
}
}
public static FileInfo[ ] ReadFile ( DirectoryInfo directory, params string [ ] varExtension)
{
List< FileInfo> TempFiles = new List< FileInfo> ( ) ;
FileInfo[ ] files = directory. GetFiles ( ) ;
if ( varExtension != null && files != null )
{
for ( int j = 0 ; j < files. Length; j++ )
{
var tempfile = files[ j] ;
if ( varExtension. Contains ( tempfile. Extension) )
{
TempFiles. Add ( tempfile) ;
}
}
}
else if ( files != null )
{
TempFiles. AddRange ( files) ;
}
if ( directory. GetDirectories ( ) != null )
{
DirectoryInfo[ ] directories = directory. GetDirectories ( ) ;
for ( int i = 0 ; i < directories. Length; i++ )
{
var tempfiles = ReadFile ( directories[ i] , varExtension) ;
if ( tempfiles != null )
TempFiles. AddRange ( tempfiles) ;
EditorUtility. DisplayProgressBar ( "Read load file" , directory. FullName, ( i * 1.0f ) / directories. Length) ;
}
}
return TempFiles. ToArray ( ) ;
}
public static FileInfo[ ] ReadFileExtension ( DirectoryInfo directory, params string [ ] varExtension)
{
List< FileInfo> TempFiles = new List< FileInfo> ( ) ;
FileInfo[ ] files = directory. GetFiles ( ) ;
if ( varExtension != null && files != null )
{
for ( int j = 0 ; j < files. Length; j++ )
{
var tempfile = files[ j] ;
if ( ! varExtension. Contains ( tempfile. Extension) )
{
TempFiles. Add ( tempfile) ;
}
}
}
else if ( files != null )
{
TempFiles. AddRange ( files) ;
}
if ( directory. GetDirectories ( ) != null )
{
DirectoryInfo[ ] directories = directory. GetDirectories ( ) ;
for ( int i = 0 ; i < directories. Length; i++ )
{
var tempfiles = ReadFileExtension ( directories[ i] , varExtension) ;
if ( tempfiles != null )
TempFiles. AddRange ( tempfiles) ;
EditorUtility. DisplayProgressBar ( "Read load file" , directory. FullName, ( i * 1.0f ) / directories. Length) ;
}
}
return TempFiles. ToArray ( ) ;
}
public static void DisplayProgressBar ( string varTitle, string varContent, float varProgress)
{
EditorUtility. DisplayProgressBar ( varTitle, varContent, varProgress) ;
if ( varProgress >= 1 )
{
EditorUtility. ClearProgressBar ( ) ;
}
}
}
[ System. Serializable ]
public class BaseDate
{
public virtual void Init ( )
{
}
}
通用脚本 CommonUI
public class CommonUI
{
public const float Tools_barPadding = 15f ;
public const float Tools_Buttonspacing = 5f ;
public static string GUI_SelectDirectory ( string varPath, string varTitle)
{
EditorGUILayout. BeginHorizontal ( ) ;
varPath = EditorGUILayout. TextField ( varTitle, varPath) ;
if ( GUILayout. Button ( "Choose.." , GUILayout. Width ( 100 ) ) )
{
varPath = EditorUtility. OpenFolderPanel ( "选择文件夹" , varPath, "" ) ;
}
if ( GUILayout. Button ( "Open File" , GUILayout. Width ( 100 ) ) )
{
if ( ! Directory. Exists ( varPath) )
{
Directory. CreateDirectory ( varPath) ;
}
Application. OpenURL ( varPath) ;
}
EditorGUILayout. EndHorizontal ( ) ;
return varPath;
}
public static string GUI_SelectFilePanel ( string varPath, string varTitle, string extension)
{
EditorGUILayout. BeginHorizontal ( ) ;
varPath = EditorGUILayout. TextField ( varTitle, varPath) ;
if ( GUILayout. Button ( "Choose.." , GUILayout. Width ( 100 ) ) )
{
varPath = EditorUtility. OpenFilePanel ( "选择文件" , varPath, extension) ;
}
if ( GUILayout. Button ( "Open File" , GUILayout. Width ( 100 ) ) )
{
string tempDirectory = Path. GetDirectoryName ( varPath) ;
if ( ! Directory. Exists ( tempDirectory) )
{
Directory. CreateDirectory ( tempDirectory) ;
}
Application. OpenURL ( tempDirectory) ;
}
EditorGUILayout. EndHorizontal ( ) ;
return varPath;
}
public static bool GUI_Button ( string varTitle)
{
if ( GUILayout. Button ( varTitle) )
{
return true ;
}
return false ;
}
public static bool GUI_Button ( string varTitle, float varwidth)
{
if ( GUILayout. Button ( varTitle, GUILayout. Width ( varwidth) ) )
{
return true ;
}
return false ;
}
public static bool EnumButton < T> ( ref T varEnum, params GUILayoutOption[ ] options)
{
EditorGUILayout. BeginHorizontal ( ) ;
GUILayout. FlexibleSpace ( ) ;
string [ ] tringname = System. Enum. GetNames ( varEnum. GetType ( ) ) ;
int count = tringname. Length;
GUILayout. Space ( Tools_barPadding) ;
for ( int i = 0 ; i < count; i++ )
{
string tempName = tringname[ i] ;
if ( GUILayout. Button ( tempName, options) )
{
object temp = ( System. Enum. Parse ( varEnum. GetType ( ) , tempName) ) ;
varEnum = ( T) temp;
return true ;
}
}
GUILayout. FlexibleSpace ( ) ;
EditorGUILayout. EndHorizontal ( ) ;
return false ;
}
public static bool CenterButton ( string text, params GUILayoutOption[ ] options)
{
GUILayout. BeginHorizontal ( ) ;
GUILayout. FlexibleSpace ( ) ;
if ( GUILayout. Button ( text, options) )
{
return true ;
}
GUILayout. FlexibleSpace ( ) ;
GUILayout. EndHorizontal ( ) ;
return false ;
}
public static bool CenterButton ( string [ ] text, out int index, params GUILayoutOption[ ] options)
{
index = 0 ;
bool onclick = false ;
EditorGUILayout. BeginHorizontal ( ) ;
GUILayout. FlexibleSpace ( ) ;
string [ ] tringname = text;
int count = tringname. Length;
GUILayout. Space ( Tools_barPadding) ;
for ( int i = 0 ; i < count; i++ )
{
string tempName = tringname[ i] ;
if ( GUILayout. Button ( tempName, options) )
{
index = i;
onclick = true ;
}
}
GUILayout. FlexibleSpace ( ) ;
EditorGUILayout. EndHorizontal ( ) ;
return onclick;
}
public static void CenterLabel ( string text, params GUILayoutOption[ ] options)
{
GUILayout. BeginHorizontal ( ) ;
GUILayout. FlexibleSpace ( ) ;
GUILayout. Label ( text, options) ;
GUILayout. FlexibleSpace ( ) ;
GUILayout. EndHorizontal ( ) ;
}
public static void CenterText ( ref string [ ] text, params GUILayoutOption[ ] options)
{
GUILayout. BeginHorizontal ( ) ;
GUILayout. FlexibleSpace ( ) ;
for ( int i = 0 ; i < text. Length; i++ )
{
text[ i] = GUILayout. TextField ( text[ i] , options) ;
}
GUILayout. FlexibleSpace ( ) ;
GUILayout. EndHorizontal ( ) ;
}
public static void LeftText ( ref string [ ] text, params GUILayoutOption[ ] options)
{
if ( text == null )
return ;
GUILayout. BeginHorizontal ( ) ;
for ( int i = 0 ; i < text. Length; i++ )
{
text[ i] = GUILayout. TextArea ( text[ i] , options) ;
}
GUILayout. FlexibleSpace ( ) ;
GUILayout. EndHorizontal ( ) ;
}
public static bool RightButton ( string text, params GUILayoutOption[ ] options)
{
GUILayout. BeginHorizontal ( ) ;
GUILayout. FlexibleSpace ( ) ;
if ( GUILayout. Button ( text, options) )
{
return true ;
}
GUILayout. EndHorizontal ( ) ;
return false ;
}
}