How to use public variables in Unity scripts put into asset bundles - c#

I've searched on this question and found a lot of info related, but some inconsistencies, and no clear answer.
In a Unity project, I'm creating asset bundles with gameobjects that have scripts (C#). I've learned how to do that, and it works.
My problem is, I ultimately need to preserve the values of the public variables in the scripts that were assigned before building the asset bundle. That is, my prefab has a script with public variables. I instance it many times with different public values for each instance. I need all that to survive the trip through the asset bundle.
My current process is thus:
Remove scripts from their game objects.
Build the scripts into assemblies
Pack game objects and assemblies into asset bundles
Download the bundles to new project
Unpack the asset bundles and Instantiate the game objects
Attach script assemblies to game objects with AddComponent()
The issue is step 1. I remove the scripts from the game objects before bundling because if I don't, it complains when unpacking that the referenced script is missing. And also, since step 6 uses AddComponent to attach it, I cant really do that if that script is already an attached component. So removing them before bundling solves those problems.
What am I missing? Where is my method at fault? I read that the public values are saved in the script reference on the game object when bundled, but of course I lose that when I remove the script components.
So, please, how do I maintain public values thru the asset bundling process? Should I NOT be removing script components before bundling? If so, how should I be managing the scripts differently? Would I need to bundle both the script assemblies AND source files? (I currently only bundle the assemblies), and wouldn't that create a redundancy when attaching the assembly?

Unity does not store assemblies in bundles. It stores only serialization data for scripts (that's your public variables, and variables with [SerializeField] attribute). But it also stores the hash of the script assembly for every script type, so you have to build bundles in the same project with the main executable to avoid missing references.
So, you don't have to remove scripts from their GameObjects, but you have to build bundles in the same unity project and rebuild them, when code changes.

Related

Multiple script and namespace errors on a previously working Unity project . Type or namespace name 'GUIColorOverride' could not be found

I had some unusual errors on a relatively small Unity project with six c# scripts written in Visual Studio. It was all working fine, and I came back to it after the weekend. Upon opening, there were 18 new errors which were preventing the code from compiling and I spent several hours trying to figure out what was going on.
The main errors were saying things like Type or namespace name 'GUIColorOverride' could not be found and associated script cannot be loaded, assign a valid script, even though they were linked and I could see the scripts had no errors in Visual Studio. I even did a full Unity uninstall and rebooted, then a clean install. This didn't fix it.
I tried a number of other things such as backing up the scripts from the assets folder and then deleting them. Opening the project and pasting them back in as 'clean' new scripts. This didn't work. I am using Unity's Cloud Collab so was even able to use this to go back to earlier pushes of the project (which had all worked). Nope.
Solved it! The issue was that the local Projects folder I had been using was connected to Google Backup and Sync. This messed with some of the files in some of the folders in PackageCache in Particular in the MyProject > Library > PackageCache > com.unity.timeline#1.2.13 > Editor > directory.
[FIX] To fix and prevent this from happening in future, I turned off Google Backup and Sync for my Unity Project(s), and am now exclusively using Unity Collab and Git. Using the specific errors that were showing in the Unity console, I located the exact path of the Unity-created files that were causing these errors in my broken project, and replaced them with the same files from a working project. This fixed all of the errors. There must have been some corrupted or partially synced files in these folders which caused Unity to have a tantrum. Hope that helps someone!
You should exclude these from the Backup!
Basically you can exclude anything that would also be excluded from git via e.g. this "official" .gitignore file for Unity projects.
Any file and folder listed here will be regenerated by Unity automatically and should not appear in any version control / Backup.
The entire Library folder in particular is part of it since its content changes quite rapidly and is recompiled every time you change a script, install/remove a package etc.
Checkout Unity Manual - Behind the Scenes for more information in detail
When backing up a project, or adding a project to a Version Control Repository, you should include the main Unity project folder, containing both the Assets
and ProjectSettings folders. All the information in these folders is crucial to the way Unity works. You should omit the Library and Temp folders for backup purposes.
To solve an already present issue with the Library folder you can most of the times fix it by simply closing Unity, deleting the entire Library folder via your file browser and opnening Unity again → Unity will rebuild the Library

Can I run BuildPipeline.BuildAssetBundles in playmode?

Hi everyone I need a help with Unity. I need to run the method 'BuildPipeline.BuildAssetBundles' after I click a button while i'm using the built software, is it possible to do? I know that this is an editor script, but is there a way to call it in a 'non-editor script?
Thanks at all
No classes that are part of the UnityEditor namespace will work in a build. Unity will simply refuse to try to do it. You can use things from AssetBundle because it's a part of UnityEngine, but you can't use BuildPipeline in a build. Unity allows the UnityEditor namespace to be include only in files that are marked as being editor files, usually by being placed in folders called "Editor" and those files are excluded from the build.
You can use BuildPipeline in playmode inside of Unity Editor's player though, but that's about it.

Cannot load AssetBundle because "it is not compatible with the newer version of the Unity runtime"

I am trying to load an AssetBundle from a file, however I get the following error:
The AssetBundle 'path\to\file' could not be loaded because it is not compatible with this newer version of the Unity runtime. Rebuild the AssetBundle to fix this error.
I build my AssetBundle as shown on the Unity wiki:
using UnityEditor;
namespace Editor
{
public class CreateAssetBundles
{
[MenuItem("Assets/Build AssetBundles")]
private static void BuildAllAssetBundles()
{
BuildPipeline.BuildAssetBundles("Assets/AssetBundles",
BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}
}
}
This generates a correct looking AssetBundle, the manifest file also looks fine.
I load the AssetBundle with the following code:
var assetBundle = AssetBundle.LoadFromFile(path);
Both the AssetBundle and the game are built with the same version of Unity, version 2017.3.1f1 (64 bit). I've also tried building both with the latest available beta build, but this did not resolve the issue.
Changing the BuildTarget to BuildTarget.StandaloneWindows64 also does not resolve the issue.
The Unity docs are outdated a bit on AssetBundles, since Unity 2017 they introduced an entire new assetbundle system, which is easier to use and works with an improved UI called the AssetBundle Browser
I've had issues myself when switching from Unity 5.x to 2017.x using assetbundles, and it actually required me to use the new assetbundle system, and build/load through that to get them to work again.
Get the Assetbundle browser:
Download the AssetBundle Browser from Unity's GitHub
Add the downloaded files to your Unity project
Go to Window
AssetBundle browser
Building an assetbundle:
here you will see two tabs, "configure" and "Build". Select the assetbundle you want to build by dragging a prefab of the object into the configure tab. you'll get a question asking if you want to build it as one big bundle or multiple seperate bundles, select whichever you prefer.
The Browser will also give a warning if multiple bundles share the same assets, and propose to make a single seperate bundle containing all shared resource, depending on how many and how big your bundles are this can save quite alot of space.
Then if you go to the "Build tab" you can select for which platform you want to build and the output path, along with some additional options such as compression type. Then all you have to do is click "Build" to build your new assetbundle compatible with unity 2017.x
Loading an Assetbundle:
Loading an assetbundle from a file is as simple as using the following piece of code: AssetBundle myAssetBundle = AssetBundle.LoadFromFile(path);
You can also load assetbundles from Memory (taking in bytes) or load directly from a stream.
An additional bonus to the new AssetBundle browser is that you can customize it however you need All files can be found in /Assets/Editor/AssetBundleBrowser/. for example I included the functionality to automatically upload all bundles to an FTP after its done building.
Edit: The Unity AssetBundle browser tool works for version 5.6 or higher.

Load serverside c# scripts and then attach a scripts to game object

I have Two question.
1. How can I load existing c# scripts from external path to my apk file.
2. How can I attach scripts in a game object.
Assuming that I create a sphere and then build an apk file. when i run my unity app, you can see the sphere,which is not moving. After building apk file, i'd like to move the sphere .
Scripts are compiled objects, so I believe you can't load them in dynamically. In addition, I believe Android wouldn't allow it anyway, as loading scripts in dynamically is considered a security risk. You could download an app, which downloads new virus-type scripts that weren't in the original code...
you can attach C# script to game object and disable it(unchecked), and after build apk file you can enable it by a button in run time,

access another script from FirstPersonController

sorry for my English!
I have scripts called "MYJoystick.cs":
public class MYJoystick : MonoBehaviour
{
public static string myX;
}
and I want to access "myX", from "FirstPersonController.cs" in unity 5. I want to write this in the Update in the FirstPersonController Like this:
private void Update()
{
MYJoystick.myX += 1;
}
but it says "The name `MYJoystick' does not exist in the current context".
How can I fix this? should i add Using In FirstPersonController? using what? what is the default namespace in unity? I also tried to add Namespace for my script, but it didn't work too!
I just had this problem.
http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html
The basic rule is that anything that will be compiled in a phase after the current one cannot be referenced.
The phases of compilation are as follows:
Phase 1: Runtime scripts in folders called Standard Assets, Pro Standard Assets and Plugins.
Phase 2: Editor scripts in folders called Editor that are anywhere inside top-level folders called Standard Assets, Pro Standard Assets and Plugins.
Phase 3: All other scripts that are not inside a folder called Editor.
Phase 4: All remaining scripts (ie, the ones that are inside a folder called Editor).
Your FPSController c# script is in the standard assets folder, this is compiled before your other custom c# scripts that are in another folder.
To fix this you could rename the 'Standard Assets' to another name, or put this folder into another folder or take the FPSController script and put this with your other scripts.
A better solution would be to copy the FPSController standard assets script and put this into your custom folder and then modify this script instead.
I tried to change the compilation order of the scripts as a better solution, but it didn't seem to work. That's only for run time with Start() and Awake() I think between scripts.

Categories

Resources