Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

C-Sharp Function that calls HTML - inspects - elements

Discussion in 'Scripting' started by Cabras-Games33, Sep 25, 2022.

  1. Cabras-Games33

    Cabras-Games33

    Joined:
    Sep 25, 2022
    Posts:
    1
    Hi everyone!

    I need to take from a specific website in HTML some numbers that are present in the source (inspects - elements - (<span class = "game-ball"> 15 </span>)) and then insert them into unity. How can I call this function?
    I insert the code that works perfectly by importing images from steam.

    Here is the code that works with: <div class = "workshopItemPreviewHolder"> <img class = "workshopItemPreviewImage" src = </div>

    I have to replace it with: <li class = "result-full__list-item">
    <span class = "game-ball"> 15 </span>
    </li>

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using System.Net;
    7. using CodeMonkey;
    8. using CodeMonkey.Utils;
    9.  
    10. public class WorkshopShowcase : MonoBehaviour {
    11.    
    12.     private List<Texture2D> workshopThumbnailList = new List<Texture2D>();
    13.     private Transform container;
    14.     private Transform thumbnailTemplate;
    15.  
    16.     private void Awake() {
    17.         container = transform.Find("containerMask").Find("container");
    18.         thumbnailTemplate = container.Find("thumbnailTemplate");
    19.     }
    20.  
    21.     private void Start() {
    22.         DownloadWorkshopShowcase();
    23.     }
    24.  
    25.     private void PrintThumbnails() {
    26.         // Clear Previous Thumbnails
    27.         foreach (Transform child in container) {
    28.             if (child == thumbnailTemplate) continue;
    29.             Destroy(child.gameObject);
    30.         }
    31.  
    32.         // Create Thumbnails
    33.         float totalWidth = workshopThumbnailList.Count * 108;
    34.         for (int i=0; i<workshopThumbnailList.Count; i++) {
    35.             Transform thumbnailTransform = Instantiate(thumbnailTemplate, container);
    36.             thumbnailTransform.gameObject.SetActive(true);
    37.  
    38.             Vector2 startingPos = new Vector2(108 * i, 0);
    39.             Vector2 pos = startingPos;
    40.             RectTransform rectTransform = thumbnailTransform.GetComponent<RectTransform>();
    41.             rectTransform.anchoredPosition = startingPos;
    42.             thumbnailTransform.GetComponent<RawImage>().texture = workshopThumbnailList[i];
    43.  
    44.             FunctionUpdater.Create(delegate () {
    45.                 if (rectTransform == null) return true;
    46.                 pos += new Vector2(-100, 0) * Time.deltaTime;
    47.                 if (pos.x < -430) pos += new Vector2(totalWidth, 0);
    48.                 rectTransform.anchoredPosition = pos;
    49.                 return false;
    50.             });
    51.         }
    52.     }
    53.  
    54.     private void RandomizeList() {
    55.         if (workshopThumbnailList == null || workshopThumbnailList.Count == 0) return;
    56.         // Randomize list
    57.         for (int i=0; i<50; i++) {
    58.             int rnd = UnityEngine.Random.Range(0, workshopThumbnailList.Count);
    59.             Texture2D tmp = workshopThumbnailList[0];
    60.             workshopThumbnailList[0] = workshopThumbnailList[rnd];
    61.             workshopThumbnailList[rnd] = tmp;
    62.         }
    63.     }
    64.    
    65.     private void DownloadWorkshopShowcase() {
    66.         // Manually curated collection
    67.         //string url = "http://steamcommunity.com/sharedfiles/filedetails/?id=1222955566"; // Hyper Knights: Battles
    68.         //string url = "http://steamcommunity.com/sharedfiles/filedetails/?id=1314094214"; // Ninja Tycoon
    69.         string url = "http://steamcommunity.com/sharedfiles/filedetails/?id=1849057378"; // Battle Royale Tycoon
    70.        
    71.         Debug.Log("Downloading Workshop Showcase...");
    72.         Get(url, (string error) => {
    73.             Debug.Log("Could not contact Steam Workshop Showcase");
    74.             Debug.Log("Error: " + error);
    75.         }, (string htmlCode) => {
    76.             Debug.Log("Steam Workshop Showcase downloaded");
    77.             // Download images
    78.             string textToFind;
    79.             int cycleProtection = 0;
    80.             while (htmlCode.IndexOf("<img class=\"workshopItemPreviewImage") != -1 && cycleProtection < 100) {
    81.                 cycleProtection++;
    82.                 textToFind = "<img class=\"workshopItemPreviewImage";
    83.                 htmlCode = htmlCode.Substring(htmlCode.IndexOf(textToFind) + textToFind.Length);
    84.                 textToFind = "src=\"";
    85.                 htmlCode = htmlCode.Substring(htmlCode.IndexOf(textToFind) + textToFind.Length);
    86.                 string imageUrl = htmlCode.Substring(0, htmlCode.IndexOf("\""));
    87.  
    88.                 GetTexture(imageUrl, (string error) => {
    89.                     Debug.Log("Failed to download thumbnail");
    90.                     Debug.Log("Error: " + error);
    91.                 }, (Texture2D texture) => {
    92.                     workshopThumbnailList.Add(texture);
    93.                     RandomizeList();
    94.                     PrintThumbnails();
    95.                     Debug.Log("Workshop showcase amount: " + workshopThumbnailList.Count);
    96.                 });
    97.             }
    98.         });
    99.     }
    100.    
    101.     public void Get(string url, Action<string> onError, Action<string> onSuccess) {
    102.         WebRequests.Get(url, onError, onSuccess);
    103.     }
    104.  
    105.     public void GetTexture(string url, Action<string> onError, Action<Texture2D> onSuccess) {
    106.         WebRequests.GetTexture(url, onError, onSuccess);
    107.     }
    108.  
    109. }


    I am attaching the image of the numbers that I would like to import from the website to unity 3d.

    Unity IMG.png

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,567
    Wow, 1994 was a fun year. I remember doing this stuff.

    Are you sure this is kosher with Steam's terms of service? You might be setting yourself up for the big old ban hammer if you're not using their API properly...
     
    Bunny83 and All_American like this.
unityunity