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,
Related
I want to make a Windows Desktop App in c#, that a user can drag and drop files (models, textures, materials) onto, which then bundles these assets into a Unity Package.
Can this be done from outside Unity using .Net, or can it only be done with Unity's own editor? If so, what NuGet package would be used as there are many Unity packages?
Many thanks.
If I understand your question correctly you wouldn't need an app for this.
Unitpackages can afaik only be created using Unity.
Unity has Command Line Arguments which allow you to export packages via a cmd call afaik like e.g.
C:Path\To\Unity.exe" -quit -batchmode -nographics -projectPath "C:Path\To\A\Unity\Project" -exportPackage "Assets" "C:Path\To\Export\File.unitypackage"
I don't know why you want that, but you can simply put the files you want into a folder as you wish and then convert to a zip file for easy transport internet if that is what you wish. I dont see the point in making a Unity Package for that.
I am trying to show post/pre build data to user in the unity game, something I want to update whenever I process a build. turns out in unity, the updated file version after pre or post build is not included in assets. It still fetches the data before pre/post build script.
Can someone help me resolve this issue.
run prebuild script and call AssetDatabase.Refresh()
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.
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.
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.