1) Addressable hot update resource type 2) local delete FBX DefaultMaterial will fail after Unity reboot 3) How to implement MeshRenderer effect 4) UGUI dynamic load Item DrawCall problem 5) Loading.CheckConsistency [Editor Only] Optimizations on the Editor
This is the 250th post of UWA Technical Knowledge Sharing. Today, we continue to select a number of issues related to development and optimization for you. It is recommended that you read for 10 minutes.
UWA Q&A Community: Answer.uwa4d.com UWA QQ Group 2:793972859 (The group is full)
Addressable
Q: Addressable can hot-update Text/ XML resources without using AssetBundle. Addressable is a hot update solution. Some resources do not want to read from AssetBundle, but would like to read from AssetBundle directly.
A: Addressable currently does not support RAW directly. It basically uses AssetBundle.
AssetBundle is used as an AssetBundle and is used as an AssetBundle. Logically, it is used as a Text, Binary, or Binary. Another idea is to do expansion.
You can see the Provider class TextDataProvider, with “file.readAllText (path);” inside. Access to resources, thus can actually achieve AddressableAssetGroupSchema and BuildScriptBase, do a RawAssetSchema and BuildScriptRawAsset extension to packaging.
Thanks to the @uwa Q&A community for providing the answer
Editor
Q: When using the Addressables system, the Default Material of AssetBundle is redundant. When you run the script on the packager, the Default Material of FBX becomes purple, but when you reopen the project, the Default Material is returned, so the redundancy is still there. (Unity 2018.4.34)
The script to delete the material:
using System; using UnityEngine; using System.Collections; using System.IO; using UnityEditor; using Object = UnityEngine.Object; class DisableMaterialImport : AssetPostprocessor { [MenuItem("Tools/Reimport All Model")] public static void ReimportAllModel() { var assetPaths = AssetDatabase.GetAllAssetPaths(); Array.Sort(assetPaths); Debug.LogWarning(string.Format("Total assets count: {0}", assetPaths.Length)); int processedCount = 0; foreach (string assetPath in assetPaths) { string normalizedAssetPath = assetPath.ToLower(); if (! normalizedAssetPath.EndsWith(".fbx") && ! normalizedAssetPath.EndsWith(".obj") && ! normalizedAssetPath.EndsWith(".3ds")) { continue; } var modelImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter; if (modelImporter == null || modelImporter.importMaterials) { continue; } AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ImportRecursive | ImportAssetOptions.ForceUpdate); Debug.Log(assetPath, AssetDatabase.LoadMainAssetAtPath(assetPath)); processedCount++; } Debug.LogWarning(string.Format("Total processed model count: {0}", processedCount)); AssetDatabase.SaveAssets(); } private static void FixedModelImport(ModelImporter modelImporter, GameObject model) { //Debug.Log("FixedModelImport "+model); var renderers = model.GetComponentsInChildren<Renderer>(true); if (renderers == null) { return; } modelImporter.importMaterials = false; modelImporter.importBlendShapes = false; modelImporter.importAnimation = false; modelImporter.isReadable = false; modelImporter.optimizeMesh = true; foreach (var renderer in renderers) { if (renderer == null) { continue; } renderer.sharedMaterials = new Material[renderer.sharedMaterials.Length]; } /* var animator = model.GetComponent<Animator>(); if(animator! =null) Object.DestroyImmediate(animator); * /}
A1: I have tested it and it won’t change back. Is it just to roll back the project? In addition, the script’s reimportAllModel is only valid for models that do not have Import Materials checked.
Thanks to the Prin@UWA Q&A community for the answers
A2: turn off the Cache Server after several attempts.
Thanks to the question@uwa question-and-answer community for providing the answer
Editor
Q: I have a requirement that objects in the level card be created indirectly through a script, rather than dragging the Prefab directly into the scene. Therefore, I need to be able to display the MeshRenderer directly in the scene and make it easy for me to select and edit.
The code looks something like this:
public class SpawnPrefab : MonoBehaviour { public GameObject prefabEntity; void Start() { Instantiate(prefabEntity); }}
The MeshRenderer has two features: 1. In Editor mode the SceneView is displayed under the Gameview; 2. 2. Objects drawn in runtime mode can be selected under Sceneview.
I’m calling graphics. drawMesh from onDrawGizMos, but it can’t be selected in SceneView, and drawing in Update can’t be shown in Editor mode. I feel like I’m trying in the wrong direction.
A: OnDrawGizMos does not draw. This function will not be called at Runtime when it is packaged.
To select an edit in the SceneView, it must be a GameObject in the scene. (Handing the MeshRenderer with the GameObject will certainly satisfy both the “SceneView editing” and “runtime script creation” requirements.)
If you want the Mesh to be drawn using the Graphics’ immediate render interface and displayed in the Editor, you can simply add an executeAlways attribute to the class, but you can’t select edit in the SceneView yet.
If you also want to implement Scene editing, you can only bind the Mesh to a Scene with a GameObject with the MeshRenderer hanging in it. The graphics.drawMesh will draw the Transform of the object exactly as this GameObject does. As shown in the figure below, the Game window is the effect drawn by the Graphics.DrawMeshNow function in the OnPostRender function, and the Scene window is the bound object used to edit and adjust. The Transform of the two is the same:
The key code is as follows:
Thanks to the Prin@UWA Q&A community for the answers
UGUI
Q: Regarding the dynamic loading of items, is this a kind of interplay? If there are a large number of items, how many drawcalls will be required? I think I have made a mistake.
I’m just using an example here, normal project elements can not be so small, if more, how to load the dynamic batch? It would be unreasonable to write a separate script to handle the elements of a dynamically loaded Item. For example, NGUI uses Depth to control the group of elements under the panel. I only know that UGUI controls the group by placing order. If this is the only way to control the group, it is not friendly to dynamic loading.
A1: Shared mesh of the same material, meet other conditions of batch. Render in adjacent order to batch, reduce drawcalls. So the key is to control the rendering order for objects that use the same material.
The following information is available for the conditions and optimization scheme: https://blog.uwa4d.com/archives/optimzation_cpu.html https://docs.unity3d.com/Manual/DrawCallBatching.html
The Unity of the factors affecting rendering order: https://zhuanlan.zhihu.com/p/55762351
Different resources control the rendering order in different ways. For example, the MeshRenderer can set the RenderQueue of the material. The ParticleSystem can set Order In Layer, Sorting Layer, etc.
The rendering order of the UGUI elements is determined by the UGUI plugin itself. What we need to do is to understand how the UGUI renders the elements and try to make the UI elements of the same material render in adjacent order. The key points are to avoid overlapping UI elements of the same layer, to make sure that the UI of the same material is drawn as the same layer, and that the UGUI is drawn layer by layer. For example, IMG in the main project is placed in the same layer, and TXT is placed in the second layer. If two IMGs overlap, one of them will be drawn as the second layer, resulting in the drawCall of TXT and IMG being drawn together.
As long as they do not overlap, elements of the same level that use the same atlas will automatically merge. If the interface is complex, you can only merge the atlas as much as possible. In the case of multiple layers, you can merge the atlas as long as you ensure that the elements with the same material use the same atlas and draw them in adjacent order. If there are many images in UGUI, DrawCall is not a good way to control it.
Thanks to the Prin@UWA Q&A community for the answers
A2: As long as the area of an Item does not overlap with the area of another Item, then all text counts as Level 2 and Image counts as Level 1, and only 2 DrawCalls are required. If you are afraid of being affected by other UI elements, you can place these items on a separate Canvas so that they will not be affected by other UI elements.
Thanks to the Xuan@UWA Q&A community for the answers
Editor
Q: Loading.CheckConsistency [Editor Only] is still running on the Editor. Q: Loading.CheckConsistency [Editor Only] is running on the Editor.
A: ReadObject deserializes an Object after it has been loaded. Once a Prefab is deserialized, there will be a large number of Objects. IntegrateAllThreadedObjects will traverse the Object, and Loading. CheckConsistency is when traversing the Object, the data consistency check.
Consistency checking, for example, for the serialized Prefab file below, checks that the fileID in the two red boxes is consistent.
Image Source:
https://www.cnblogs.com/luguoshuai/p/12323186.htmlAs explained in this post, if the two fileIDs do not match, there will be a CheckConsistency error.
Why do consistency checks only in the Editor, but not in the Runtime after packaging? My guess is that all objects have already been validated at packaging time, so Runtime does not need validation and avoids the time-consuming validation.
The proof of this is shown in the following image, where the checkConsistency step is included in the error stack of one of the packages:
Image Source:
https://networm.me/2019/06/23/unity-has-stopped-working/
Thanks to the Prin@UWA Q&A community for the answers
Cover from: Pixel Sorting at https://lab.uwa4d.com/lab/5dd4587f8bab6aaf02db4018
That’s all for today’s sharing. Life, of course, is long and knowledge is long. The questions you may see are just the tip of the iceberg in the long development cycle, and we have already prepared more technical topics for you to explore and share on the UWA Q&A website. Welcome you who love progress to join us. Maybe your method can solve others’ urgent need. And the “stone” of another mountain can attack your “jade”.
Website: www.uwa4d.com Official Technology Blog: blog.uwa4d.com Official Q&A Community: Answer.uwa4d.com Official Technology QQ Group: 793972859 (The original group is full)