How to avoid two files in project view when creating a custom importer in Unity3D? - unity3d

NOTE: I'm casting a wider net than Unity Answers, my original question can be found here.
I've created a ProTools CueSheet importer. It uses OnPostprocessAllAssets() to detect a files in the project that have a .cuesheet extension. It then runs those cuesheet files through my parser. This generates a ScriptableObject which is then turned into an asset via Database.CreateAsset().
The problem is, this leaves me with two files, the original cuesheet and the newly generated asset. Is there any way I can generate the asset in such as way that the original cuesheet acts as the asset, the same way textures or FBX files do?
public class CueSheetPostProcessor : AssetPostprocessor {
static string extension = ".cuesheet";
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
foreach (string assetPath in importedAssets) {
if (assetPath.EndsWith(extension, StringComparison.OrdinalIgnoreCase)) {
string newPath = assetPath + ".asset";
Session session = CueImporter.Load(assetPath);
AssetDatabase.CreateAsset(session, newPath);
AssetDatabase.SaveAssets();
}
}
}
}

I would create a PropertyDrawer for the Serializable class CueSheet such that it accepts the .cueSheet TextAsset or path to the .cueSheet file and create an instance of the class CueSheet and assign it or modify it.

Rather than create a new asset, have you tried using AssetDatabase.AddObjectToAsset instead? An file in Assets can "hold" multiple assets and multiple asset types. When importing a .fbx, the output in the project inspector is several things: a GameObject tree (ie, a prefab), some models, maybe a rig, and so on. All these are "contained" in the processed .fbx

Related

Is it possible on monogame to load content files to the Content Manager at run time?

The reason I want to do this is that I'd like users to be able to create their own racetrack and save it as an image. Users would then be able to select their image and race on their track.
I'm thinking of using the following code to test if the file exists
using System.IO;
public static bool TrackExists(string fileName)
{
return File.Exists($#"Content\Tracks\{fileName}.xnb");
}
If it doesn't exist in the pipeline, I'd like it to be added and built so it can then be used in the project.
How should I go about doing this?
if (File.Exists(path))
{
tex.FromFile(_graphicsDevice, path);
_game.ChangeState(new RaceState(_game, _graphicsDevice, _content, tex));
}
If you need to load an image file as a Texture2D, you need to use the static method Texture2D.FromFile() as follows
myTex2D = Texture2D.FromFile(GraphicsDevice, "path/to/file");
As a rule of thumb, you cannot use the pipeline to load files dynamically. Nevertheless, it is not necessary that you have to use it. AFAIK the pipeline is the best and fastest way to load the static content of your game. There will be methods to import your custom files as content dynamically, so don't worry just because you're not using the pipeline. There are many types in Monogame (like Texture2D) that support dynamic loading from files.
EDIT:
Just to clarify, The method is a static member of Texture2D class, so you should be using it as follows:
if (File.Exists(path))
{
tex = Texture2D.FromFile(_graphicsDevice, path);
//...Rest of your code
}

Unity Scriptable Objects issue

I have a list in RoomSO that contains premade ButtonSOs that i created in the editor and dragged and dropped into the list. RoomSO is also premade (made in the editor) Then at runtime i create an instance of a ButtonSO and tried adding it to RoomSO buttons. I look at the RoomSO in the editor and i get "Type mismatch". I canĀ“t understand why?
RoomSO script:
[CreateAssetMenu(fileName = "New Room", menuName = "Rooms/Room")]
public class RoomSO : ScriptableObject
{
public List<ButtonSO> buttons;
public void AddButton()
{
ButtonSO bt = (ButtonSO) ScriptableObject.CreateInstance<ButtonSO>() as ButtonSO;
bt.buttonText = "Hello";
buttons.Add(bt)
}
}
My ButtonSO script:
[CreateAssetMenu(fileName = "New Button", menuName = "Rooms/Button")]
public class ButtonSO : ScriptableObject
{
public string buttonText;
}
You issue isn't really an "issue".
What happens is that usually Unity expects a ScriptableObject asset reference assigned to the fields in the Inspector. Because the actual use-case of ScriptableObject is to have certain exchangeable data/behaviour container assets.
What you are doing is creating instances on runtime. These instances are only stored in the temporary memory since you never actually store them as assets into your project.
You will see the type mismatch in the Inspector, but actually this means there is a valid reference - otherwise it would say either None (ButtonSO) or Missing (ButtonSO), it only won't be saved once the Playmode ends. The later Missing (ButtonSO) you will see after ending the Play Mode since the List<ButtonSO> will still exist, also the items within it - but the according references you created during runtime will be destroyed.
Your runtime code should still work as expected as long as the Play mode is running.
The same happens btw if you assign e.g. a GameObject field within a ScriptableObject with a reference from the Scene during runtime.
In general though: If you create these instances on runtime - why do they need to be ScriptableObjects? You could just use a normal class.

Iterating and reading text files from folder on Android

I made a word game app for android in Unity, where the player has to find a word from a category previously loaded to the game.
The way I load the categories is:
There is a folder named Categories, inside Assets, I run through the folder and read each text file as a category.
The categories are stored in a dictionary where the key is name the of the file and the value is every line of the file as an array element.
It worked well on the PC however no luck on android. Tried changing the path to
"public string categoriesDirectoryPath = Application.persistentDataPath +"Categories";" still does not work.
Original path was "Assets/Categories"
Code for initiating the dictionary with the file values is (Happens on GameManager's Awake()):
private Dictionary<string, string[]> createCategories(string directoryPath)
{
Dictionary<string, string[]> categories = new Dictionary<string, string[]>();
string[] categoryPaths = Directory.GetFiles(directoryPath);
foreach (string path in categoryPaths)
{
if (!path.EndsWith("meta")) {
Debug.Log(path);
string categoryName = Path.GetFileNameWithoutExtension(path);
Debug.Log(categoryName);
string[] categoryData = File.ReadAllLines(path).ToArray();
categories.Add(categoryName, categoryData);
}
}
return categories;
}
Is there a way of iterating the folder and reading the text files that were in Assets/Categories after building the APK?
Is there a way of iterating the folder and reading the text files that
were in Assets/Categories after building the APK?
No.
If you want to access from the project, in a build, you have two options:
1.Put the file a folder named "Resources" then use the Resources to read the file and copy it to the Application.persistentDataPath path. By copying it to Application.persistentDataPath, you'll be able to modify it. Anything in the "Resources" is read only.
2.Put the file in the StreamingAssets folder then use UnityWebRequest, WWW or the System.IO.File API to read it. Atfer this, you can copy it to the Application.persistentDataPath.
Here is a post with code examples on how to do both of these.
3.AssetBundle(Recommended due to performance and loading reaons).
You can build the file as AssetBundle then put them in the StreamingAssets folder and use the AssetBundle API to read it.
Here is a complete example for building and reading AssetBundle data.

Unity5 AssetBundle functions obsolete?

So I have read through the Unity5 AssetBundle changes, and understand them all perfectly. My problem is that a lot of functionality has been made 'obsolete', but the functions seem to still work and the Unity5 documentation is actually using obsolete functions.
My main concern is, how would I now, in Unity5, take a directory of prefabs and turn all of them into their own separate AssetBundles seperately? Not just one AssetBundle containing everything, but rather each built into it's own separate AssetBundle?
Ideally I would use the BuildPipeline.BuildAssetBundle function. But unity5 says that is obsolete. But if you look here:
http://docs.unity3d.com/500/Documentation/Manual/managingassetdependencies.html
They are using that function in the manual.
Also it says the CollectDependencies option is obsolete and no longer needed. But I removed it from my code and then Unity spit out the error:
Please specify BuildAssetBundleOptions.CollectDependencies or collect GameObject's components and pass as 'assets' parameter.
The new BuildPipeline.BuildAssetBundlestakes a AssetBundleBuild array as input. You can create a AssetBundleBuild[] and fill it with all the prefabs you want, as separate bundles:
//Create an array for 2 different prefabs.
AssetBundleBuild[] buildMap = new AssetBundleBuild[2];
//Make a buildMap for the first prefab.
AssetBundleBuild buildInfo1 = new AssetBundleBuild();
//The name of the bundle that the prefab will be saved as.
buildInfo1.assetBundleName = bundle1Name+".unity3d";
//Only one file for this prefab.
string[] prefabs1 = new string[1];
//The full path to the prefab.
prefabs[0] = prefab1Path;
buildInfo1.assetNames = prefabs1;
buildMap[0] = buildInfo1;
AssetBundleBuild buildInfo2 = new AssetBundleBuild();
//The name of the bundle that the prefab will be saved as.
buildInfo2.assetBundleName = bundle2Name+".unity3d";
//Only one file for this prefab.
string[] prefabs2 = new string[1];
//The full path to the prefab.
prefabs[0] = prefab2Path;
buildInfo2.assetNames = prefabs2;
buildMap[0] = buildInfo2
//Save the prefabs as bundles to the "bundleFolder" path.
BuildPipeline.BuildAssetBundles(bundleFolder, buildMap)

TypeLite generate external modules?

I am trying to generate external modules rather than a type definition file. I believe I need to do the following:
Change the extension of the file to .ts instead of .d.ts.
Generate one file per module.
Add the key word "Export" in front of each interface and enum.
I was easily able to change the extension of the file by changing the "output extension" setting in the tt file.
I cannot figure out how to split the modules into separate files.
I cannot figure out how to add the Export key word to each interface.
TypeLITE doesn't support generating multiple files. This feature has been requested by several users, but I am not aware of a simple way to generate multiple files from the single tt file.
export keyword can't be added without changing source code of the library (TsGenerator.cs). This is very specific requirement, so I probably won't add it to the library.
TypeLite is a good project but lacking in Documentation and examples, it's open source so anyone can contribute and make it better.
As for creating a file per class i solved it using the code below.
private static void GenerateTypeScriptContracts(string assemblyFile, string outputPath)
{
// Clean TS Folder
System.IO.DirectoryInfo di = new DirectoryInfo(outputPath);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
// --
var assembly = Assembly.LoadFrom(assemblyFile);
// If you want a subset of classes from this assembly, filter them here
var models = assembly.GetTypes();
foreach (var model in models)
{
var generator = new TypeScriptFluent()
.WithConvertor<Guid>(c => "string")
.WithMemberFormatter((identifier) => Char.ToLower(identifier.Name[0]) + identifier.Name.Substring(1));
generator.ModelBuilder.Add(model);
// Generate TS interface definitions
var tsClassDefinitions = generator.Generate(TsGeneratorOutput.Properties | TsGeneratorOutput.Fields);
File.WriteAllText(Path.Combine(outputPath, "I" + model.FullName.Replace("ProjectName.DtoModels.", "") + ".ts"), tsClassDefinitions);
}
}