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. Dismiss Notice

How to build a Project with AssetDatabase

Discussion in 'Asset Database' started by MarsterWaldi, Dec 1, 2020.

  1. MarsterWaldi

    MarsterWaldi

    Joined:
    Nov 19, 2020
    Posts:
    10
    Hello guys,
    i´m new at Unity and i got a Problem.
    I have a database with information and i want to Create Scriptable Objects at runtime. When i Export that project i got an error: " Unity Editor is not in the current context" to fix the Export i use #if UnityEditor. The export is an Webgl project and i want that the export is creating the Scriptable Object and create object´s in the scene.

    Is there another way except Asset database to create Scriptable Objects and load Scriptable Objects from an folder??

    Code:
    #if UNITY_EDITOR

    UnityEditor.AssetDatabase.CreateAsset(so, "Assets/Resources/" + this.name + "_" + z + ".asset");
    UnityEditor.AssetDatabase.SaveAssets();
    UnityEditor.AssetDatabase.Refresh();

    if (clear == true)
    {
    z--;
    clear = false;
    }
    #endif
     
  2. HueSamai

    HueSamai

    Joined:
    May 7, 2018
    Posts:
    13
    What are the scriptable objects currently used for?
     
  3. MarsterWaldi

    MarsterWaldi

    Joined:
    Nov 19, 2020
    Posts:
    10
    @HueSamai I save my Data from my Database into the Scribtable objects to get the Information tomy Prefabs.
    I try it before without the Scriptable Objects,but then he duplicate some information.
     
  4. HueSamai

    HueSamai

    Joined:
    May 7, 2018
    Posts:
    13
    Can you not use a class that saves the information and then sends it to a prefab? For instance, instead of adding a new asset you just create a new variable, like
    ScriptableObjectName variableName = new ScriptableObjectName();


    Of course you would have to replace the ScriptableObjectName with the class name of the scriptable object.

    You could then add information to the variable to store.
     
  5. MarsterWaldi

    MarsterWaldi

    Joined:
    Nov 19, 2020
    Posts:
    10
    In the Awake Methode i create an inctance of this object.
    This is the full code from the part where i create the Scribtable object.

    In an other Script i add the Scriptable Object to an Prefab.

    Code:

    {
    if(z<anzahl)
    {

    var so = ScriptableObject.CreateInstance<getData>();

    // Debug.Log(beschreibung[zahl]);
    if (create == false)
    {
    so.ID = getID[z];
    so.nameText = beschreibung[z];
    so.BildImage = url[z];
    so.X = posX[z];
    so.Z = posZ[z];

    #if UNITY_EDITOR

    UnityEditor.AssetDatabase.CreateAsset(so, "Assets/Resources/" + this.name + "_" + z + ".asset");
    UnityEditor.AssetDatabase.SaveAssets();
    UnityEditor.AssetDatabase.Refresh();

    if (clear == true)
    {
    z--;
    clear = false;
    }
    #endif

    }
    else
    {
    // transform.position += new Vector3(posX[z], 0, posZ[z]);
    var temp = Instantiate(prefab, transform.position, Quaternion.identity);

    string oName = this.name + "_" + z;

    pfad = oName + ".asset";
    temp.name = oName;
    temp.tag = "Respawn";
    // transform.Parent(this.transform);
    temp.transform.SetParent(this.transform);

    }

    z++;
    }
    }

    Code Where i load the Scriptable object and save the new Prefab :

    #if UNITY_EDITOR
    info = AssetDatabase.LoadAssetAtPath<getData>(x);


    #endif

    nameText.text = info.nameText;
    X = info.X;
    Z = info.Z;
    // transform.LookAt(transform.parent);
    StartCoroutine(GetImage());

    #if UNITY_EDITOR
    transform.position += new Vector3(X, 0, Z);

    string localPath = "Assets/Resources/Prefab/" + gameObject.name + ".prefab";
    PrefabUtility.SaveAsPrefabAssetAndConnect(gameObject, localPath, InteractionMode.UserAction);

    #endif


    I want to have an Export WebGl which is creating the Scriptable Objects( or somethink where i can do the same) an set an Prefab into the sceen
     
  6. MarsterWaldi

    MarsterWaldi

    Joined:
    Nov 19, 2020
    Posts:
    10
    this 2 emty object got the same script and when i do it like this:

    ScriptableObjectName variableName = new ScriptableObjectName();

    the first Script will refresh the information before i can transfer it into an object or i am wrong?
     

    Attached Files:

  7. HueSamai

    HueSamai

    Joined:
    May 7, 2018
    Posts:
    13
    Instead of creating the scriptable object and storing the values, then loading the values and assigning them to a prefab, can't you just load the information and then set the values. On the script where you load the scriptable object data (I assume this is for the prefab), have a global public variable called X and Z. Then instead of saying creating a scriptable object, just assign the values. Here is an example.
    Code (csharp):
    1.  
    2. var temp = Instantiate(prefab, transform.position, Quaternion.identity);
    3.  
    4. string oName = this.name + "_" + z;
    5.  
    6. pfad = oName + ".asset";
    7. temp.name = oName;
    8. temp.tag = "Respawn";
    9. // transform.Parent(this.transform);
    10. temp.transform.SetParent(this.transform);
    11. temp.GetComponent<ScriptName>().X = posX[z];
    12. temp.GetComponent<ScriptName>().Z = posZ[z];
    13.  
    then in the script for prefabs:
    Code (csharp):
    1.  
    2.  
    3. //Declare variables
    4. var X;
    5. var Z;
    6.  
    7. ...
    8.  
    9. // transform.LookAt(transform.parent);
    10. StartCoroutine(GetImage());
    11.  
    12. transform.position += new Vector3(X, 0, Z);
    13.  
    14.  
    Hopefully this helps. This is not intended to be copied just to give you an idea of what to do. Also I would like to point out, that you are using UnityEditor.AssetDatabase and PrefabUtility, as they were not intended to be used. That is why I removed the saving Prefab bit of the code. Why do you want to save the prefabs, so that I can help you find an alternative.
     
    MarsterWaldi likes this.
  8. HueSamai

    HueSamai

    Joined:
    May 7, 2018
    Posts:
    13
    The information will be lost, if it is not a global variable. So if it is created in a function, it will be created everytime that function is run, making the information not carry over. But if it is a global variable than it will not.
     
  9. MarsterWaldi

    MarsterWaldi

    Joined:
    Nov 19, 2020
    Posts:
    10
    @HueSamai Tank you very much that is working now :D
     
  10. HueSamai

    HueSamai

    Joined:
    May 7, 2018
    Posts:
    13
    That's great to hear! :)