Use Unity code to generate a GameObject through Xml configuration-a pitfall from the previous two days, unitygameobject
1. Resources. Load (path), path is not an absolute path, but a relative path relative to "Resources! For example:
To Instantiate, the code should be as follows:
string man1Path = "Research/FBX/man1/";GameObject weapon = Instantiate(Resources.Load(man1Path + "char_weapon_ani", typeof(GameObject))) as GameObject;
(I tried it many times before it was successful ......) 2. I caught a sigh of relief when I read Xml on the Internet last night. I practiced method 1 and somehow fell into the trap. So I went back and sorted out my ideas. I remember that I did not write this in the past, either case1: find another parsing method, either case2: After Class serialization, copy the xml file and deserialize it. I tried two libraries, one of which is System. xml. the Descendants () method is useful in (method 1). The result is IEnumerable and cannot be operated. I think this method is feasible, but I cannot read the value = because the code is also deleted, I skipped this section. ==||| so I switched to Sytem later. xml (case 1). XmlNodeList XmlNode SelectSingleNode () is easier to understand ~ Paste some code:
XmlElement root = doc. DocumentElement; // obtain all <character> ...... </Character> XmlNodeList charactersNodes = root. GetElementsByTagName ("character"); // traverse it and continue to operate foreach (XmlNode characterNode in charactersNodes ){......}
3. Add the problem background of GameObject position through code: GameObject human includes the GameObject body, and the body contains human and weapon. We expect human. transform. position = (, 1), body. transform. position = (, 0 ). After the body. transform. parent = human. tranform is set, the body. transform. position is automatically changed to (-1, 0,-1 ). Suppose I set body. transform. position = (, 0), weapon. transform. position to (-,-1 )...... = ....... So suppose I have generated five human users with different positions. We can see that all the images overlap. = waiting for resolution. After the solution is complete, let's make a change ...... Updated: solution: You should not set the position, but set the localPosition to go with the position of the parent ~ 4. For the mesh, Material does not seem to have been attached after obtaining it. Continue to study ......