Skip to content

Instantly share code, notes, and snippets.

@s2kw
Created March 29, 2013 10:15
Show Gist options
  • Save s2kw/5270049 to your computer and use it in GitHub Desktop.
Save s2kw/5270049 to your computer and use it in GitHub Desktop.
Path string for unity. This class can't store file and file's expansion. ..and test.
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class PathString{
[MenuItem("TEST PathString")]
static void TEST_PathString(){
string _test_target_path = @"help/ui/a";
PathString pathString = new PathString(_test_target_path);
if( pathString.correctlyPath(3) == pathString.fullpath ){
Debug.Log(string.Format( "OK \n{0}\n{1}",pathString.correctlyPath(3),pathString.fullpath));
}else{
Debug.Log(string.Format( "OUT \n{0}\n{1}",pathString.correctlyPath(3),pathString.fullpath));
}
_test_target_path = @"help/ui/a";
if( pathString.correctlyPath(3) == _test_target_path ){
Debug.Log(string.Format( "OK! \n{0}\n{1}",pathString.correctlyPath(3),_test_target_path));
}else{
Debug.Log(string.Format( "OUT! \n{0}\n{1}",pathString.correctlyPath(3),_test_target_path));
}
_test_target_path = @"help/ui/";
if( pathString.correctlyPath(2) == _test_target_path ){
Debug.Log(string.Format( "OK!! \n{0}\n{1}",pathString.correctlyPath(2),_test_target_path));
}else{
Debug.Log(string.Format( "OUT!! \n{0}\n{1}",pathString.correctlyPath(2),_test_target_path));
}
_test_target_path = @"help/";
if( pathString.correctlyPath(1) == _test_target_path ){
Debug.Log(string.Format( "OK!!! \n{0}\n{1}",pathString.correctlyPath(1),_test_target_path));
}else{
Debug.Log(string.Format( "OUT!!! \n{0}\n{1}",pathString.correctlyPath(1),_test_target_path));
}
}
Public class PathString{
char SPLIT = ('/');
private string[] directory;
public string fullpath{
get{
return correctlyPath(directory.Length);
}
set{directory = value.Split(SPLIT);}
}
/* constructor */
public PathString(string path){
// Debug.Log(string.Format("SPLIT:{0}",SPLIT));
string[] str = path.Split(SPLIT);
// for(int i = 0;str.Length>i;++i){ Debug.Log(str[i]); }
directory = str;
}
public string correctlyPath( int correct = 0 ){
string path = "";
int i = 0;
while( i < correct )
{
path = path + directory[i];
++i;
// if not end dir, add path.
if( directory.Length != i ) path += @"/";
}
return path;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment