Development Environment
Windows 7
Unity 5.3 and later
Objective
Use unity built-in editor such as assetdatabase.load or animatorcontroller.createanimatorcontrolleratpath API for file operations, often encountered loading resources are null, or reported that the path does not exist!
After debugging a breakpoint, it is found that most of the errors are due to the existence of two delimiters for the path: "/" and "\".
We use the System.IO.Path API to get the path, in fact, the "\" separated path.
We open the Explorer under Windows, the path to a directory or file is: E:\Code\GameFramework\ or \\192.168.80.100\XXX\
But using the Unity API, when printing Application.datapath, print out: E:/xxx/client/trunk/project/assets, so it's clear that its path and windows are reversed, So when we use a path that does not conform to Unity's specifications, it is often reported that the resource failed to load.
For example, an FBX path is: assets/art/characters/wing/fbx_3005/[email protected] , and if your input path or stitching path does not conform to the specification, it is very likely that the file will fail to load.
Normalize paths
Provides a way to format a path into Unity's readable path format:
/// <summary> ///format the path into asset standard format/// </summary> /// <param name= "FilePath" ></param> /// <returns></returns> Public Static stringFormatassetpath (stringFilePath) { varNewFilePath1 = Filepath.replace ("\\","/"); varNewFilePath2 = Newfilepath1.replace ("//","/"). Trim (); NewFilePath2= Newfilepath2.replace ("///","/"). Trim (); NewFilePath2= Newfilepath2.replace ("\\\\","/"). Trim (); returnnewFilePath2; }
Unity's Assetdatabase path format