I always pirated PDFs of my textbooks, but in the few cases where I couldn’t find anything online (typically when the book is niche and very new), I would always wait until I knew that I actually needed the book, because it was frustrating how often this meme came true.
I had this one professor I was really grateful for though. He was a big open-source guy, apparently used to contribute to freebsd and postgres, and he went out of his way to find open-source textbooks for all of his classes.