Search Unity

[Editor Scripting] Find a mesh in a grup of mesh in unity editor

Discussion in 'Scripting' started by marpione, Jan 31, 2018.

  1. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    Hi All,

    So I'm making an editor script for my scriptable objects and what I want to do is to automatically find all related assets to that scriptiable object by it's ID. I can find materials textures ect. But I'm having problems with Meshes. So basically my models in a group of mesh. Each grup has two meshes one is the single mesh and other one is the group mehes. You can think of them as oranges. They have one orange model and group of orange models. So What I'm trying to do is to get them by their name. I want find all the meshes that contains the ID and "_single" when I use AssetDataBase.FindAsset it only retuns ID + ".fbx".

    I created a local variable when I search for these objects and load al the meshes that matches with the ID. Than I Debug their name and they all returned ID + "_group" + ".fbx" I can't seem to get the fbx with the name "_single" at all.

    You can find the code, log, and how my fbx es imported below

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class ScriptableObjectCreator{
    5.  
    6. void ValidateProduct()
    7.     {
    8.         object[] objs = AssetDatabase.FindAssets(productData.productID);
    9.         for (int i = 0; i < objs.Length; i++)
    10.         {
    11.  
    12.             Mesh snmesh = AssetDatabase.LoadAssetAtPath<Mesh>(AssetDatabase.GUIDToAssetPath(objs[i].ToString()));
    13.             Debug.Log(snmesh.name);
    14.  
    15.             if(AssetDatabase.GUIDToAssetPath(objs[i].ToString()).Contains("_tek"))
    16.                 productData.SingleMesh = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(objs[i].ToString()), typeof(Mesh)) as Mesh;
    17.  
    18.             if (AssetDatabase.GUIDToAssetPath(objs[i].ToString()).Contains("_grup"))
    19.                 productData.GroupMesh = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(objs[i].ToString()), typeof(Mesh)) as Mesh;
    20.  
    21.             if (productData.mainMaterial == null)
    22.                 productData.mainMaterial = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(objs[i].ToString()), typeof(Material)) as Material;
    23.  
    24.             string oMatName = "Assets/ArafVRPackage/Models/ProductModels/Materials/" + productData.productID + opMatName +  ".mat";
    25.             if (AssetDatabase.GUIDToAssetPath(objs[i].ToString()) == oMatName && productData.optionalMaterial == null)
    26.                 productData.optionalMaterial = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(objs[i].ToString()), typeof(Material)) as Material;
    27.  
    28.             //string aName = "Assets/ArafVRPackage/Textures//ProductTextures/" + productData.productID + "_a.png";
    29.             if (AssetDatabase.GUIDToAssetPath(objs[i].ToString()).Contains("_a.png"))
    30.                 productData.AlbodeTex = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(objs[i].ToString()), typeof(Texture2D)) as Texture2D;
    31.  
    32.             //string mName = "Assets/ArafVRPackage/Textures/ProductTextures/" + productData.productID + "_m.png";
    33.             if (AssetDatabase.GUIDToAssetPath(objs[i].ToString()).Contains("_m.png"))
    34.                 productData.MetalicTex = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(objs[i].ToString()), typeof(Texture2D)) as Texture2D;
    35.  
    36.             //Debug.Log(AssetDatabase.GUIDToAssetPath(objs[i].ToString()) + " ----- " + oMatName);
    37.             //Debug.Log(AssetDatabase.GUIDToAssetPath(objs[i].ToString()));
    38.         }
    39.     }
    40. }
    Findmes question for forms.png
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Not looked too deply into your code, but I think your problem is that you're expecting different assets for the two meshes, but it is only one file with both meshes inside.
    If you do LoadAllAssetsAtPath to find all Meshes in your fbx, you should be able to iterate and find the one you're looking for.
     
  3. marpione

    marpione

    Joined:
    Apr 16, 2016
    Posts:
    26
    OMG how can I miss it. It worked thank you I changed the code a bit but it works perfectly. Thank you so much ThermalFusion

    Code (CSharp):
    1. void ValidateProduct()
    2.     {
    3.         object[] objs = AssetDatabase.FindAssets(productData.productID);
    4.         var meshes = AssetDatabase.LoadAllAssetsAtPath("Assets/ArafVRPackage/Models/ProductModels/" + productData.productID +".fbx");
    5.  
    6.         for (int i = 0; i < meshes.Length; i++)
    7.         {
    8.             if (meshes[i].GetType() == typeof(Mesh) && meshes[i].ToString().Contains("_tek"))
    9.             {
    10.                 productData.SingleMesh = meshes[i] as Mesh;
    11.             }
    12.  
    13.             if (meshes[i].GetType() == typeof(Mesh) && meshes[i].ToString().Contains("_grup"))
    14.             {
    15.                 productData.GroupMesh = meshes[i] as Mesh;
    16.             }
    17.         }
    18. }
     
    ThermalFusion likes this.