Summary of problems and solutions encountered in unity development

In the process of developing games, we will always encounter some problems, but we have solved them without recording the solutions. The time interval is long. If we encounter them again later, we will have to go through another pit..., so I will record them here first, and I will encounter them later. You can see the solution directly here:

This article is updated from time to time. If you encounter a pitfall, update it once.

ugui related:

1. After the uui anchor point pivot and anchor box anchor are dynamically modified, the problem of dynamically setting localPosition will cause the actual coordinates to be different from the set coordinates:

Just use anchoredPosition of RectTransform to set the coordinates and sizeDelta to set the size.

RectTransform rect = img.gameObject.GetComponent<RectTransform>();
rect.anchorMin = new Vector2(0, 0);
rect.anchorMax = new Vector2(0, 0);
rect.pivot = new Vector2(0, 0.5f);
rect.anchoredPosition = new Vector3(0, 64.5f);
rect.sizeDelta = new Vector2(80, 80);
2. For the Toggle controlled by the uui component Toggle Group, there will be two selected Ison=true

I just discovered this problem. The code can set the Ison state of the Toggle's SetActive to false. However, if among the multiple Toggles controlled by the Toggle Group, if there is a Toggle with the SetActive of the Toggle in the false state, the Toggle Group cannot control this Toggle.
If you use for to loop through Toggles and look for Toggles where Ison is true, then you may find multiple Toggles where Ison is true... You
should add Toggle.gameObject.activeSelf == falsefiltering when traversing, otherwise there will be logic errors.

Editor development related

1. Must be used when deleting GameObject
GameObject.DestroyImmediate(gameobject, true)

Method can be used to delete components, parameter 1: gameobject component object, parameter 2: whether to allow resource destruction.
Cannot be used: GameObject.Destroy(gameobject), this cannot delete resources in Editor mode

2. Instantiate the object: Instantiate

The instantiated object in editor mode cannot be saved and needs to be added: EditorUtility.SetDirty(obj)method

GameObject obj = GameObject.Instantiate(levelPrefab, levelNode.transform);
EditorUtility.SetDirty(obj);

Unity component related

Light :

Set the light shadow type: (ps: There is no parameter for this.gameObject.GetComponent().shadowType==)

this.gameObject.GetComponent<Light>().shadows = LightShadows.Soft;

Miscellaneous:

1. Some mobile phones may have black edges at the top.

The phone may be a full-screen phone and needs to be added to AndroidManifest.xml:

<meta-data android:name="android.max_aspect" android:value="2.2"/>

Reference: How does unity Android adapt to full screen?
Use the "Resolution and Presentation" tab in the Player Settings window to set the game's screen resolution to adapt to different display devices. Set the game window mode and adjust the window size and position as needed. General setting parameters:
Insert image description here

Guess you like

Origin blog.csdn.net/QO_GQ/article/details/130063610