- Home /
Export unity-package on Run time?
Hi, I want to export a game object made on runtime using c# scripting. Mainly I want that a game object may be a cube to be made on runtime and then that game object to be exported as a unity package by simple some button click but I want this to happen on runtime[editor / in playmode]. May this can be done using Reflection? Please help me out.
Answer by Grish_tad · Nov 23, 2017 at 12:10 PM
Just call createUnityPackage() function, when button is clicked. Script will create unitypackage from all files included in "Assets/Prefabs/" folder and save it in "Assets/" folder with gameobject name. You can modify script for your use.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using UnityEditor;
public class UnityPackager : MonoBehaviour {
public static void createUnityPackage()
{
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
Instantiate(obj, new Vector3(0, 0, 0), Quaternion.identity);
string filePath = "Assets/Prefabs/";
FileInfo file = new FileInfo(filePath);
file.Directory.Create();
string prefabfilename = filePath + obj.name + ".prefab";
UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab(prefabfilename);
PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
string exportPath = "Assets/" + obj.name + ".unitypackage";
AssetDatabase.ExportPackage(prefabfilename, exportPath,
ExportPackageOptions.IncludeDependencies | ExportPackageOptions.Recurse);
}
}
This worked perfectly. See my answer below about the future. I am still 2018.2.2f1 and can not afford to update at the moment
Answer by giantkilleroverunity3d · Jan 27, 2019 at 07:47 PM
@Grish_tad @uzairf But not in 2018.3: deprecated. Now you must find another way. I am looking into runtime creation of asset bundles.
I also found that after i disable scripts on child objects the child object scripts are enabled when I pull the large prefab back into the hierarchy. But during the previous play mode the scripts are disabled. So the setting is getting reversed.
Do you found a solution for the runtime creation of asset Bundles? $$anonymous$$y Project lets you import a obj and I need to "share" it with all other clients of the Project. So somehow the "Importer" needs to upload the gameobject and the rest needs to download it somehow.
@fraeri During runtime I drag the newly generated gameobject to the assets/prefab folder. This definately saved the correct state of the GO. Later I can drag that into the hierarchy. Runtime import is a step I have to do next.
@giantkilleroverunity3d - nice, but that is not an option for me because the user creates the GO during runtime outside of Unity. Got any ideas for the next step? $$anonymous$$aybe we can solve it together
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Export objects to a .3DS file at runtime 1 Answer
Export cad formats from unity3d 0 Answers
How to export an fbx file with assimp that can be used in Unity? 0 Answers