Implementation of Unity3d Minimap and other proportional mappings (ii) control the movement of small maps by Rawimage UV information

Source: Internet
Author: User

In the previous article, we introduced the image simple implementation of the player's movement and map to the small map: the previous link click Open link

This article is shared by rawimage to achieve the scaling of small maps: the final implementation of the effect of the player's movement will control the rawimage of the UV information changes so that rawimage move, showing the final effect:

The adjustment of the specific UI here I do not introduce more, the interface layout adjustment can look at an article here just replace the image with Rawimage to achieve, with the panel plane to replace remember Add collider OH

In general, we use Rawimage to achieve sprite animation is also through the size of UV information adjustment

Uvrect:x Y represents the starting point of the scale; W h represents the width and height of the scale

WH is used to adjust the business scope of a small map: for example, here I debug the WH set to 0.25,0.25 will be rawimage reduced by 0.25, under the canvas always show the picture 1/4.

XY is used to control the movement of small maps and is also updated in real time with the player's location.

Using System.Collections;
Using System.Collections.Generic;
Using Unityengine;

Using Unityengine.ui;
    public class Testmap:monobehaviour {public Transform player;
    Public Transform Myterrain;
    Collider Myterraincol;
    Recttransform Litmap;
    Recttransform Sonrect;
    float widthrate;
    float heightrate;
    Rawimage Rawimage;
    Vector3 Tmpangle;
    float Timecount = 0;

    Rect Rowrect;
        void Start () {myterrain = Gameobject.findgameobjectwithtag ("Terrian"). Transform;
        Myterraincol = myterrain.getcomponent<collider> ();
        Litmap = transform.parent.getcomponent<recttransform> (); Sonrect = transform.
        Getcomponent<recttransform> ();

    Rawimage = transform.parent.getcomponent<rawimage> ();
        } void Update () {timecount + = Time.deltatime;
            if (Timecount > 0.5f) {timecount = 0;
        Updatepos (); }} void Updatepos () {WidtHrate = (player.transform.position.x-myterrain.position.x)/myterraincol.bounds.size.x;
        Heightrate = (player.transform.position.z-myterrain.position.z)/myterraincol.bounds.size.z; This is relative to the rawimage offset of the WH ratio obtained by UV, such as WH is 0.25, then the displacement offset of 0.5-1/8=0.375 wh is 0.5 so the displacement offset is 0.5-1/4=0.25 rowrect = rawimage.u
        Vrect;
        Rowrect.x = Widthrate + 0.375f;
        Rowrect.y = Heightrate + 0.375f;

        Rawimage.uvrect = Rowrect;
        Tmpangle = Sonrect.localeulerangles;
        Tmpangle.z = 90-player.localeulerangles.y;

    Sonrect.localeulerangles = Tmpangle;

 }
}
But the above operation to find a problem is that when moving to the boundary will be exposed, beyond the field of view, of course, the real project is definitely not allowed this, you can steal a lazy general RPG scene of the edge of these may be exposed to the range, so that the player limit the range is not moving on the edge of the scene is not a solution

At this point we have to add the constraints, there are two ways to achieve:

Scenario One: You can control the range of WH in the range to be exposed, the farther the boundary, the greater the visible range of the adjustable wh;

Scenario two: By adjusting when you want to go beyond the scope of business to control the UV information of the XY no longer change, but by adjusting the player's mobile location update, the implementation method and image update location of the same operation,

But this limited condition personally feel very cumbersome, to add multilayer judgment, here only to provide general ideas I made a few judgments after the discovery can limit the field of view will not be exposed, but the location of the Playericon will have a tiny different step, test it yourself.

The following is my vision does not go beyond the limits of the field of view, the effect is I attached to the motion diagram operation, perhaps you have a better solution welcome to communicate


In the next article I will introduce the third way to implement the map of equal scale mapping------through ScrollView to achieve

Using System.Collections;
Using System.Collections.Generic;
Using Unityengine;

Using Unityengine.ui;
    public class Testmap:monobehaviour {public Transform player;
    Public Transform Myterrain;
    Collider Myterraincol;
    Recttransform Litmap;
    Recttransform Sonrect;
    float widthrate;
    float heightrate;
    Rawimage Rawimage;
    Vector3 Tmpangle;
    float Timecount = 0;
    Rect Rowrect;

    Vector2 tmppos = Vector2.zero;
        void Start () {myterrain = Gameobject.findgameobjectwithtag ("Terrian"). Transform;
        Myterraincol = myterrain.getcomponent<collider> ();
        Litmap = transform.parent.getcomponent<recttransform> (); Sonrect = transform.
        Getcomponent<recttransform> ();
        

    Rawimage = transform.parent.getcomponent<rawimage> ();
        } void Update () {timecount + = Time.deltatime;
            if (Timecount > 0.5f) {timecount = 0;
        Updatepos ();
 }   } void Updatepos () {widthrate = (player.transform.position.x-myterrain.position.x)/Myterraincol.bo
        unds.size.x;
       Heightrate = (player.transform.position.z-myterrain.position.z)/myterraincol.bounds.size.z;
       Rowrect = Rawimage.uvrect;
       Rowrect.x = Widthrate +0.375f;
       Rowrect.y = Heightrate +0.375f;


        Rawimage.uvrect = Rowrect;

        Rowrect = Rawimage.uvrect; if (widthrate + 0.375f > 0f && widthrate + 0.375f < 0.75f && heightrate + 0.375f > 0 && H
            Eightrate + 0.375f < 0.75f) {rowrect.x = widthrate + 0.375f;
        Rowrect.y = Heightrate + 0.375f;  } else {if (widthrate + 0.375f <= 0f) {rowrect.x =
            0;
            } else if (widthrate + 0.375f >= 0.75) {rowrect.x = 0.75f; } else if (Heightrate + 0.375f; = 0.75) {rowrect.y = 0.75f;
            } else if (heightrate + 0.375f <= 0f) {rowrect.y = 0;
            }//tmppos.x = litmap.sizedelta.x * widthrate;
            TMPPOS.Y = Litmap.sizedelta.y * heightrate;
        Sonrect.anchoredposition = Tmppos;
        } rawimage.uvrect = Rowrect;
        Debug.Log ("rowrect===" +rowrect);
        Debug.Log ("rawimage.uvrect===" + rawimage.uvrect);
        Tmpangle = Sonrect.localeulerangles;
        Tmpangle.z = 90-player.localeulerangles.y;

    Sonrect.localeulerangles = Tmpangle;
 }

   
}





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.