Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Saving selfcreated Object permanently as Asset?

Discussion in 'Editor & General Support' started by BPR, Mar 29, 2013.

  1. BPR

    BPR

    Joined:
    Jan 4, 2013
    Posts:
    56
    Hi,

    i am trying to save a GameObject/Prefab which was created on runtime to my project assets, unfortunately when i call AssetDatabase.CreateAsset () the prefab is created in my project folder - BUT it's spawned so i have two objects in the scene (one which i used to call AssetDatabase.CreateAsset () and the other one which is named after the prefab just created) this second object won't disappear after i leave playmode and i get an error: "Some objects were not cleaned up when closing the scene":neutral:
    Furthermore after leaving playmode the prefab in my project folder becomes empty only the transform component remains:eek:
    I'm looking for a way to store a GameObject permanently so i can create it once and then instantiate it in further game sessions...

    Thanks for your support,

    BPR

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5. using UnityEditor;
    6.  
    7. public class AssetCreation : MonoBehaviour {
    8.    
    9.     Texture2D pico;
    10.     GameObject oob;
    11.     public Texture2D display;
    12.     // Use this for initialization
    13.     void Start () {
    14.         pico = new Texture2D(display.width,display.height);
    15.        
    16.         for (int i =0; i < display.width; i++){
    17.             for (int j = 0; j < display.height; j++){
    18.             pico.SetPixel(i,j,new Color(1f,0.0f,0.0f,1f)); 
    19.             }
    20.         }
    21.         pico.Apply();
    22.        
    23.        
    24.        
    25.         oob = new GameObject();
    26.         oob.AddComponent("MeshFilter");
    27.         MeshFilter filler =(MeshFilter) oob.GetComponent(typeof(MeshFilter)) ;
    28.         filler.mesh = new Mesh();
    29.         Vector3 [] vert = {new Vector3(1f,0f,0f),new Vector3(1f,0f,1f), new Vector3(0f,0f,0f)};
    30.         int [] tries = {0,1,2};
    31.         Vector2 [] uvs = {new Vector2(1f,0f),new Vector2(1f,1f),new Vector2(0f,0f)};
    32.        
    33.         filler.mesh.vertices = vert;
    34.         filler.mesh.triangles = tries;
    35.         filler.mesh.uv =uvs;
    36.        
    37.         filler.mesh.RecalculateNormals();
    38.        
    39.        
    40.         oob.AddComponent("MeshRenderer");
    41.         MeshRenderer rend = (MeshRenderer) oob.GetComponent(typeof(MeshRenderer));
    42.        
    43.         rend.material.mainTexture = null;
    44.         rend.material.mainTexture = pico;
    45.        
    46.        
    47.        
    48.         AssetDatabase.CreateAsset (oob, "Assets/test.prefab");
    49.         AssetDatabase.SaveAssets();
    50.        
    51.    
    52.     }
    53.  
    54. }
    55.  
     
  2. kiranmaya

    kiranmaya

    Joined:
    May 27, 2010
    Posts:
    218
    for that u need to write editor scripts .
    if you are not dealing with complex things.u can just drag and drop the gameObject to the asset floder at runtime ,it will create a prefab of that particular state of the gameObject .
     
  3. BPR

    BPR

    Joined:
    Jan 4, 2013
    Posts:
    56
    Hi, thanks i will try it this way but drag and drop is no option since in the game this is supposed to happen automaticly ;)
     
  4. aayars

    aayars

    Joined:
    May 15, 2008
    Posts:
    46
    The PrefabUtility class might be of use here (possibly followed by an AssetDatabase.Refresh).