How to load a mesh from an asset bundle? - unity3d

I'm trying to load a mesh with scripts to replace another mesh. I'm not doing a Unity project but rather a visual studio project with unity references.
My code for loading the asset bundle and getting the mesh is:
var vshojoBundle = AssetBundle.LoadFromFile("vshojo");
if (vshojoBundle == null) meshNames.Add("bundle not read");
GameObject silverObject = vshojoBundle.LoadAsset("SilverAssets.obj");
MeshFilter silverNewMesh = silverObject.GetComponent<MeshFilter>();
meshNames.Add(silverNewMesh.mesh.name);
MeshNames is just a list that later gets written in a log file to help me figure out things. When trying to get the name of the mesh in the last line, I get the following error: NullReferenceExpection: System.NullReferenceException: Object reference not set to an instance of an Object. The same exception occurs if I don't use that line to write on log and skip to the mesh replacement, when I replace the mesh I get the same error.

Related

Spatial Object Mesh Observer: Mesh Data

For an academy project I have to find parts of a object (location / angle relative to the object ). The object is marked with a QR-code.
Currently I'm stuck on the basics. I scanned a room and load this room with "Spatial Object Mesh Observer".
But this observer gives no relevant information:
The observer does not attempt to find 3D model LODs when sending the meshes to the application.
Someone a hint where I could start?
Scanned Room with a box ( object to find ):
var observer = CoreServices.GetSpatialAwarenessSystemDataProvider<IMixedRealitySpatialAwarenessMeshObserver>();
// Loop through all known Meshes
foreach (SpatialAwarenessMeshObject meshObject in observer.Meshes.Values)
{
Mesh mesh = meshObject.Filter.mesh;
var vertices = mesh.vertices;
// mesh.vertexCount -> 15978
// mesh.vertices -> empty
// mesh.triangles -> empty
// Do something with the Mesh object
}
Edit: 29.04.2021
Unity: Unity 2019.4.21f1
MRTK: 2.6.1
Everything seems to be loaded correctly and the triangles are recognized in an external tool.
However, this information is not available in Unity.
From our understanding, you can't obtain the vertices and triangles property of Spatial Mesh at the runtime. So, we try to make some modifications based on the SpatialAwarenessMeshDemo scene (Assets/MRTK/Examples/Demos/SpatialAwareness/Scenes) to reproduce this issue. We added the following code to the ToggleObservers() method and made it to be invoked when the sphere is clicked.
var observer = CoreServices.GetSpatialAwarenessSystemDataProvider<IMixedRealitySpatialAwarenessMeshObserver>();
foreach (SpatialAwarenessMeshObject meshObject in observer.Meshes.Values)
{
Mesh mesh = meshObject.Filter.mesh;
Debug.Log(mesh.vertexCount);
Debug.Log(mesh.vertices);
Debug.Log(mesh.triangles);
}
After our test, everything works well both in the HoloLens2 Device and Unity Holographic Remoting, it always outputs the expected value. So we recommend that you check for updates in Settings to see If there is a system update available for HoloLens 2. Then follow our steps to make a simple test to see if that works. This may help you locate the problem in your project.
I moved the code to "void OnBecameVisible()", now the information are available

Getting warning "Failed to create agent because there is no valid Navmesh" using Unity

I'm using procedural mesh geometry and I've set Navigation Static 'On' in the Navigation Window. I've also set the mesh generator to 'Static' in the inspector. I also baked my enemy's agent, but it is still throwing the warning "Failed to create agent because there is no valid NavAgent" that is most likely causing the error: "SetDestination" can only be called on an active agent that has been placed on a NavMesh. That error is happening every frame. What can I do to create a better NavMesh?
Links:
Procedural Mesh Geometry
https://docs.unity3d.com/Manual/GeneratingMeshGeometryProcedurally.html
Nav Mesh
https://docs.unity3d.com/Manual/nav-NavigationSystem.html

Unity is always throwing a Win32 Exception [duplicate]

so I'm new to Unity and I've been trying to test the scene with the script attatched to a character. However, it keeps saying "The associated script cannot be loaded. Please fix any compile errors and assign a valid script." It also says that the name of the file could be different from the name in the code but it isnt, and also it says that the code could be missing MonoBehaviour Scripts. It wont even allow me to attach the script to characters because it cant find the script class.
I've copied and downloaded character movement codes from the internet but they didnt work either. I've also tried deleting and re-making the CS files but that didnt work either. Even adding empty scripts to characters didnt work unless i do it from "Add component"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
SpriteRenderer sprite;
Rigidbody2D rigid;
// Start is called before the first frame update
void Start()
{
sprite = GetComponent<SpriteRenderer>();
rigid = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (Input.GetKey("d"))
rigid.velocity = new Vector2(2, 0);
else if (Input.GetKey("a"))
rigid.velocity = new Vector2(-2, 0);
}
}
There are also these errors in Unity if that helps
I think your class name is different from file name.
Unity apparently can't handle apostrophes (single-quote ') in the directory name of the editor. You need to get rid of the apostrophe in your directory name. Once you make that change, Unity should be able to build the scripts as intended.
Edit: This has been fixed in more recent versions - see https://issuetracker.unity3d.com/issues/scripts-do-not-get-compiled-if-the-unity-editor-path-contains-apostrophes for reference
First, it is recommended to use "Add component" to create a script, if you want to attach it to a GameObject, as it automatically imports necessary libraries. Implementing MonoBehaviour is necessary for adding a script to a GameObject.
Second, FixedUpdate() should not be set to private, it does not need an access modifier, just like Start(), see https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html.
Third, the errors in your first screenshot seem to imply that there is a problem with your Unity installation. Try reinstalling it and make sure that the Editor you install matches your operating system (64 or 32 bit?).
Fourth, the second screenshot is shown when you use any obsolete libraries or classes, which does not seem to be the case in the script you shared.
Hope that helps.
It's basically because you deleted some script or renamed it or degraded unity version. you might have to reassign the script at the required position/component.
Note: Make sure that class name is the same as the script name in unity.

Why does this code to stash babylonjs meshes cause an error?

I am loading a .obj file containing meshes of numerals. I want to stash each mesh against its name so that I can create instances later.
The code works fine if I remove the line that is actually stashing the mesh in 'originals'. However when I leave it in it appears that an exception is thrown and caught inside Babylon with the message "BJS - [12:17:50]: Unable to load assets from /threedee/InputMonoNumerics.obj: Error in onSuccess callback".
originals = {}
BABYLON.SceneLoader.LoadAssetContainer(assetPath, assetName, scene, (container) ->
container.addAllToScene()
for i in[0...scene.meshes.length]
mesh = scene.meshes[i]
if (mesh.name.startsWith('numeral'))
character = mesh.name.charAt('numeral_'.length)
originals["_#{character}"] = mesh
console.log("This line is never reached.")
mesh
)
This is coffeescript source - but the transpiled code looks exactly as you would expect
Yes - much thanks to caffeinated.tech - obviously (now you come to mention it) try and catch to see the underlying error!
... which was that the debug statements I had put in to help were throwing an error by calling JSON.stringify on Babylon Mesh objects - which are circular - I think because they have a reference to their parent which of course has a reference back to the Mesh.
It is no longer clear what the original problem was - but that's not a problem.

How to set TrackableName for an ImageTarget at runtime in Vuforia SDK for Unity 3D?

I am using Vuforia's SDK for Unity 3D platform. I am trying to set the TrackableName dynamically at runtime. I found a code
GameObject prefab = Instantiate(imageTarget) as GameObject;
ImageTargetBehaviour imgTargetBeh = prefab.GetComponent();
imgTargetBeh.DataSetPath = "QCAR/Test.xml";
imgTargetBeh.TrackableName = "Daddy";
imgTargetBeh.mInitializedInEditor = true;
myModel.transform.parent = prefab.transform;
The problem is I am getting error with DataSetPath, TrackableName, mInitializedInEditor.
The ImageTargetBehaviour class does not have above mentioned properties.
So how/where to set the properties?
Thanks,
Sris
I can't yet global comment, so I'll ask here for more info and update this when I have it.
The second line worries me, because you're not telling Unity what kind of component to get. Try GetComponent(typeof(ImageTargetBehaviour)) instead of the null parameters.
The problem is I am getting error with DataSetPath, TrackableName, mInitializedInEditor. The ImageTargetBehaviour class does not have above mentioned properties. So how/where to set the properties?
If the ImageTargetBehaviour class doesn't have these properties, then why are you trying to access them? Does the documentation instruct you to do so? If so, then something is wrong with your ImageTargetBehaviour.js script.