- Home /
ReImport in c# of GameObjects only for Scene Objects, not assets?
Hi, i want this to reimport specific GameObjects, in case they're mess-up through manipulation, now I've got this issue now that it only find's the real Path of the Asset, when they're somewhere in the Scene (/Assets/Folder/Folder/GameObject.fbx) , else I've got the Error: Could not find a Part of the Path Exception..
So for a temporary Solution i've checked for scene GameObjects only, but that's not what I really want achieve.
Basically I want to get the full Path from a GameObject, and then reload it via LoadAssetAtPath()
EDIT: Code "Debug Mode" here's the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
[ExecuteInEditMode]
public class ReImport : MonoBehaviour {
public bool Re_Import;
public string realPath;
public string selectedPath;
public string correctFile;
public GameObject Imported;
// Use this for initialization
// Update is called once per frame
void Update () {
if(Re_Import){
reimp(Imported);
Re_Import = false;
}
}
void reimp(GameObject go){
if(go.scene.IsValid()){
realPath = Application.dataPath;
realPath.Remove(realPath.Length -6);
selectedPath = realPath + AssetDatabase.GetAssetPath(go);
string[] fileEntries = Directory.GetFiles(selectedPath,"*",SearchOption.AllDirectories);
for(int i = 0; i<fileEntries.Length; i++){
string file = fileEntries[i];
file = file.Replace("\\","/");
file = file.Remove(0,realPath.Length);
if(file.Contains(PrefabUtility.GetCorrespondingObjectFromSource(go).name) && !file.Contains(".meta")){
correctFile = file;
// AssetDatabase.ImportAsset("Assets"+file);
Debug.Log(" Object was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+correctFile+"</i></color>");
}
}
}
else{
realPath = Application.dataPath;
realPath.Remove(realPath.Length -6);
selectedPath = realPath + AssetDatabase.GetAssetPath(go);
correctFile = AssetDatabase.GetAssetPath(go);
Debug.Log(" Object was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+correctFile+"</i></color>");
}
}
}
Thanks for any Advice dan
I am not sure if this is the problem, but isnt selectedPath the path to one of the specific objects in the directory? shouldnt it be just the path to the directory rather to one specific object?
I just don't get it why there's this difference between asset database.getassetpath().
if - A: the asset is in the Scene and the ReImport function is called on Object. and - B: the asset is not in the Scene and the ReImport function is called on the Object
I now did this twice on the same Object. Once I assign the Object from inside the scene and once I assigned it from the AssetFolder => see photo.
I changed the code' a bit: just for debug.(Edited my Question)
Object from Scene
Object From Asset Folder
well to sum up, to get the Asset Path for Scene Object's you need to dig in Deep and search every object! and for Asset Path for Objects in the Asset Folder it's enough to call:
AssetDatabase.GetAssetPath($$anonymous$$yGameObject);
Or is there an easier way for Scene Objects as well, which I'm missing totally?
@xxmariofer the whole Array string[] fileEntries is in my case 5566 entries, all with the correct path!
Can you test using the InstanceID of the object rather than passing the reference to the object ? selectedPath = realPath + AssetDatabase.GetAssetPath(go.GetInstanceID(); I dont really understand that unity behaviour this is the only thing i could think about.
Answer by dan_wipf · Jan 22, 2019 at 05:18 AM
thank's to @xxmariofer for your wisdom! Finally I' figured out what the Problem was. If you want to know the correct exact Path from a Scene Object you need to get the CorrespondingObjectFromSource first and then cast GetAssetAtPath.
here's the working code for ReImporting either Scene Objects or Assets from a Folder!
public static void ReImport(this GameObject go){
string selectedPath;
if(go.scene.IsValid()){
Object realGO = PrefabUtility.GetCorrespondingObjectFromSource(go);
selectedPath = AssetDatabase.GetAssetPath(realGO);
AssetDatabase.ImportAsset(selectedPath);
Debug.Log(" Scene Object was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+selectedPath+"</i></color>");
}else{
selectedPath = AssetDatabase.GetAssetPath(go);
AssetDatabase.ImportAsset(selectedPath);
Debug.Log(" Asset was succesfully reimported at: "+"<color=#e0771a><i>"+"Assets"+selectedPath+"</i></color>");
}
}