U3D_RPG Game Development Items Management (iii) _U3D game development

Source: Internet
Author: User

The next step is the last function of a temporary pause--when the mouse hovers over a grid of objects, display the corresponding item information
In the second related article there are two empty method bodies, in fact, in order to achieve today's features reserved. So the next code is written in the Inventoryitemgrid script.
And then let's do the function 1. declaring variables

is still declaring the corresponding variable to store the corresponding data, in the u3d of the object column of the UI to create a panel, can be used as a text background, and then create the Text,panel under the panel is obtained through the u3d, and text my side is obtained through the Code, The text here is the information used to write specific items.

    Public Image goodsinformationcontainer;//a container for storing information about items in which text files can display information public
    float showTime = 3;//Item information display


    private Text goodsinformation_text;//used to display items information
    private float time=0;//Timer, item information display for a period of time to disappear
    private bool Isshowinformation = false;//identifies whether to start displaying item information
2. Create showgoodsinformation ();

Let's define a method showgoodsinformation (); Here I would like to mention how to write the UI through the code location, because the item information displayed in the location must be next to the object, when the mouse hovers over the item, it should be displayed next to the corresponding items information, So when we let the panel show up, we need to determine its position. So this method requires a parameter, that is, the lattice position parameter Recttransform

    <summary>///Find the item in the dictionary, and then display the item information///</summary>///<param name= "center" > show the location of the items ;/param> private void Showgoodsinformation (Recttransform center) {//if ID is not equal to 0, it means that the lattice is an item if (th Is.id!= 0) {goodsInformationContainer.gameObject.SetActive (true);//Make panel display Goodsinforma Tioncontainer.gameobject.getcomponent<recttransform> (). anchoredposition = center.anchoredposition + new Vector2 (342.4f, -261.7f);//panel display position Objectinfo info = new Objectinfo ();//To store the information of the item info = Objects Info._instance. Getobjectinfobyid (ID)//Find this item in the dictionary and put it in the info goodsinformation_text = Goodsinformationcontainer.getcomponentinchil Dren<text> ()//Get the text under the Panel///write the article information to the text switch (id/1000) {/ Because items are four digits, the highest level of drugs is 1, the highest level of equipment is 2, and so on, so according to id/1000 can tell what kind of goods, and then display the corresponding information case 1:objectinfo_dru G Info_drug= (ObjectinFo_drug) info; Goodsinformation_text.text= "Item Name:" +info.name+ "\ Gyrus amount:" +info_drug.hp+ "back to the blue amount:" +info_drug.mp+ "\ n Sale Price:" +info_
                    Drug.price_sell;
                Break
                    Case 2:objectinfo_equip Info_equip = (objectinfo_equip) info;
                    Goodsinformation_text.text = "Item Name:" + info_equip.name + "\ n Sale Price:" + Info_equip.price_sell;
                Break
                    Case 3:objectinfo_mat Info_mat = (objectinfo_mat) info;
                    Goodsinformation_text.text = "Item Name:" + Info_mat + "\ n Sale Price:" + Info_mat.price_sell;
            Break }
        }
    }

The Vector2 in that line of code that determines where the panel is displayed needs to try it out, find the offset and add it, because the parameter we're wearing is the position of the grid UI, and the panel shown is offset by this position. 3. Use of Showgoodsinformation ()

Well, with the above method, when do we call it? At first we defined a Boolean isshowinformation, of course it was useful, and when it was true it was possible to invoke the method we wrote in the update () method. And when is isshowinfomation going to make it real? Obviously, when the mouse hovers over the grid,
Then we need to get our inventoryitemgrid to the Ipointerenterhandler and Ipointerexithandler interfaces, and then implement their approach

    <summary>
    ///Implementation interface method, when the mouse is located on the grid occurs
    ///</summary>
    ///<param name= "EventData" ></ param>
    void Ipointerenterhandler.onpointerenter (Pointereventdata eventdata)
    {
        isshowinformation = true;

    }
    <summary>
    ///Implementation interface method, when the mouse left the grid occurs
    ///</summary>
    ///<param name= "EventData" >< /param>
    void Ipointerexithandler.onpointerexit (Pointereventdata eventdata)
    {
        time+=showtime;
    }

What is this time+=showtime going to do? In fact, when the mouse hovers over the items, display the time of the goods information is limited, such as hovering in the grid, it shows the article information, can not always be shown, here, when Time>3, that is, after three seconds, The panel is hidden again, and when the mouse leaves the grid, this realizes an early disappearance to facilitate the display of the items on the grid next to the mouse hover. 4. Update () method content

Directly on the code:

    void Update ()
    {
        if (isshowinformation)
        {
            showgoodsinformation (this. Getcomponent<recttransform> ())//Call method display panel
            time + = time.deltatime;//start cumulative
            ///If the time shown is up, hide the panel, Reset the time by changing the identifier
            if (Times > ShowTime)
            {
                isshowinformation = false;
                Time = ShowTime;
                GoodsInformationContainer.gameObject.SetActive (FALSE);}}
    

So far "U3D_RPG game development of the goods management" temporarily ended

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.