SoFunction
Updated on 2025-03-08

Unit implements mouse-holding screenshots to select area

The examples in this article share with you the specific code of unity to select the area screenshot for your reference. The specific content is as follows

private int capBeginX;
private int capBeginY;
private int capFinishX;
private int capFinishY;
 
public Image showImg;
 
// Use this for initialization
void Start () {
    
  }
  
  // Update is called once per frame
  void Update () {
    if ( (0)) {
      Vector3 mousePos = ;
      Vector2 beginPos = new Vector2 (, );
      capBeginX = (int);
      capBeginY = (int);
    }
 
    if ( (0)) {
      Vector3 mousePos = ;
      Vector2 finishPos = new Vector2 (, );
      capFinishX = (int);
      capFinishY = (int);
      //Recalculate the intercepted position      int capLeftX = (capBeginX < capFinishX) ? capBeginX : capFinishX;
      int capRightX = (capBeginX < capFinishX) ? capFinishX : capBeginX;
      int capLeftY = (capBeginY < capFinishY) ? capBeginY : capFinishY;
      int capRightY = (capBeginY < capFinishY) ? capFinishY : capBeginY;
 
      Rect rect=new Rect(capLeftX,capLeftY,capRightX,capRightY);
      StartCoroutine( Captrue (rect));
    }
  }
 
  IEnumerator Captrue(Rect rect){
 
    int t_width =  (capFinishX - capBeginX);
    int t_length =  (capFinishY - capBeginY);
 
    yield return new WaitForEndOfFrame ();
    Texture2D t = new Texture2D(t_width , t_length,TextureFormat.RGB24, true);//need     Set the image saving format correctly 
    (rect, 0, 0, false);//Read pixels according to the set area; note that the lower left corner is the origin to read    (); 
    byte[] byt = (); 
    ( +  + ".png", byt); 
 
    Sprite target =  (t, new Rect(0, 0, t_width, t_length), );
     = target;
  }

The editor will share with you a piece of code to implement the screenshot function of Unity for your reference:

public class ScreenShot : MonoBehaviour 
{

  void OnScreenShotClick()
  {
    //Get the current system time     now = ;
    string times = ();
    //Remove the spaces before and after    times = ();
    //Replace the slash with horizontal bar    times = ("/", "-");

    string fileName = "ARScreenShot" + times + ".png";
    //Judge whether the platform is an Android platform    if ( == )
    {
      //The parameters are screen width screen height texture format whether to use mapping      Texture2D texture = new Texture2D(, , TextureFormat.RGB24, false);
      //Read the map      (new Rect(0, 0, , ), 0, 0);
      //App screenshot      ();
      //Serialize the object      byte[] bytes = ();
      //Set the path of the mobile phone folder to be stored      string destination = "/sdcard/DCIM/Screenshots";
      //If the folder does not exist      if (!(destination))
      {
        //Create this folder        (destination);
      }
      string pathSave = destination + "/" + fileName;
      (pathSave, bytes);
    }
  }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.